# Performance & Profiling Tips for Beginner Code

**URL:** <https://discourse.julialang.org/t/performance-profiling-tips-for-beginner-code/100585>\
**Category:** Performance\
**Tags:** performance, profiling\
**Created:** [June 20, 2023, 2:56am UTC](https://discourse.julialang.org/t/performance-profiling-tips-for-beginner-code/100585 "2023-06-20T02:56:38Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![physh](https://avatars.discourse-cdn.com/v4/letter/p/8e8cbc/32.png) [@physh](https://discourse.julialang.org/u/physh)\
**Post date:** [June 21, 2023, 2:31am UTC](https://discourse.julialang.org/t/performance-profiling-tips-for-beginner-code/100585/8 "2023-06-21T02:31:15Z")

</div>

Dear all, many thanks already for all your hints, this is exactly what I was hoping to get. I will try to answer them all:

1a)

> [@nsajko](#):
>
> In some places you use this expression, however it is mathematically equivalent to `16 * pi^2`, which is surely more accurate. In fact the latter expression happens to evaluate to the correctly rounded `Float64` representation of 16 \pi^216π216 \pi^2.

This is simply a reminiscent of the formula I derived by hand/Mathematica and stems from some spherical harmonics. When writing such code, I try to stay with the original form in order to not introduce any typo and for easier debugging. As expected, changing it to 16 \pi^2 did not really make any relevant difference.

1b)

> [@nsajko](#):
>
> In some places you use constructions like `A = A + B`. The problem with this is that `A + B` needs to be allocated. You could avoid the allocation by doing something like this instead `A .+= B`. Also see [this](https://docs.julialang.org/en/v1/manual/performance-tips/#More-dots:-Fuse-vectorized-operations) section of Performance Tips in the Manual.

I tried this by changing my code to

```julia-auto
Z .+= tril(Z,-1)';
X .+= transpose(tril(X,-1));
Y .+= transpose(tril(Y,-1));

```

however this changed the benchmark result from

```julia-auto
julia> @btime include("PerformanceTest.jl")
  1.010 s (12749484 allocations: 1.34 GiB)

```

to

```julia-auto
@btime include("PerformanceTest.jl")
  1.078 s (12789251 allocations: 1.33 GiB)

```

I don’t really understand why this actually _increases_ the number of allocations? (The memory is however slightly less…)

1c)

> [@nsajko](#):
>
> I like to do profiling in the (VS) Code editor with the Julia extension

Unfortunately, due to some [bug](https://discourse.julialang.org/t/bug-memory-leak-at-startup/96944/1), I experience some memory leak when activating the Julia extension in VSCode. But I will keep it in mind when this bug might be fixed in the future.

1. 

> [@lmiq](#):
>
> A good idea is to check for the performance of each function separately […] When these are functions called multiple times within loops, that is important, and easier to understand than the complete profile.

Is there a way to see and analyze the number of calls to this functions within one execution?

1. 

> [@Salmon](#):
>
> Small arrays where the size is known at compile time like that should usually be made by using StaticArrays which will avoid allocations and also have many optimized routines for linear algebra.  
> […]  
> Here is an example of what you can get by using static arrays:

Wow, that is quite the improvement! Thank you for the hint with using StaticArrays. I am pretty sure that I have read about this once in the performance tips or somewhere else, but I guess it was not so revealing to me until I have applied it to actual code of myself. As you showed,

```julia-auto
@btime X,Y,Z = mat_xyz(var_params, d_arr, abcvals; mu_g=1.0, prefac=-10.0, theta = 0.0);
  92.730 ms (19 allocations: 14.65 MiB)

```

which is a big improvement. I will certainly be using StaticArrays in the future.

However I am curious, when calling the entire script, why does

```julia-auto
@time X,Y,Z = mat_xyz(var_params, d_arr, abcvals; mu_g=1.0, prefac=-10.0, theta = 0.0);
@time evals,evecs = eigen(X+Y,Z);

```

yield

```julia-auto
  0.553434 seconds (249.05 k allocations: 30.053 MiB, 2.41% gc time, 71.89% compilation time)
  0.281798 seconds (20 allocations: 14.695 MiB)

```

even after several executions? Why is it recompiling the function every time?

---

_[View the full topic](https://discourse.julialang.org/t/performance-profiling-tips-for-beginner-code/100585)._
