Skip to content

Commit

Permalink
Make "Write functions, not just scripts" more prominent in the perfor…
Browse files Browse the repository at this point in the history
…mance tips (#38052)

* Make recommendation " Write functions, not just scripts" more prominent in the performance tips.

* Update doc/src/manual/performance-tips.md
  • Loading branch information
mschauer authored Feb 21, 2021
1 parent 3129a5b commit 9c31a1b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion doc/src/manual/performance-tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
In the following sections, we briefly go through a few techniques that can help make your Julia
code run as fast as possible.

## Performance critical code should be inside a function

Any code that is performance critical should be inside a function. Code inside functions tends to run much faster than top level code, due to how Julia's compiler works.

The use of functions is not only important for performance: functions are more reusable and testable, and clarify what steps are being done and what their inputs and outputs are, [Write functions, not just scripts](@ref) is also a recommendation of Julia's Styleguide.

The functions should take arguments, instead of operating directly on global variables, see the next point.

## Avoid global variables

A global variable might have its value, and therefore its type, change at any point. This makes
it difficult for the compiler to optimize code using global variables. Variables should be local,
or passed as arguments to functions, whenever possible.

Any code that is performance critical or being benchmarked should be inside a function.

We find that global names are frequently constants, and declaring them as such greatly improves
performance:
Expand Down

0 comments on commit 9c31a1b

Please sign in to comment.