Performance & Profiling Tips for Beginner Code

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^2.

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 section of Performance Tips in the Manual.

I like to do profiling in the (VS) Code editor with the Julia extension, with the commands @profview and @profview_allocs. Usually no additional packages are necessary for the profiling as the Julia extension bundles relevant code. You get an interactive (try hovering over the elements, clicking on them, etc.) flamegraph, and can also open files with highlighted costly lines of code (I definitely recommend this). Just don’t forget to run the profiling one time in advance, just to allow Julia to compile all functions.

Also see the Julia extension docs for profiling: Profiler · Julia in VS Code

3 Likes