Conclusion
Optimisation is a very broad topic, and we are only able to cover the introduction to this topic here. Many of the tips discussed in this chapter are applicable to other languages. Many tips specific to Julia are given in the “Performance Tips” section of the Julia manual, which are certainly worth reading, but the main points are included in this chapter.
We have opted not to talk about specific optimisations by using the correct algorithm for a task, as this is often too specific to a certain problem. Instead, it should be your responsibility as the developer to choose the appropriate algorithm for the job. You can then use the techniques in this chapter to make your implementation of that algorithm as performant as possible, using a single core.
When writing high performance code, it is imperative that you first optimise the existing code as described in this chapter, before moving onto optimising via parallelism. The next few chapters will cover the basics of parallelism over the main paradigms available to programmers today. It is important to remember that the main source of speed up comes from optimising the serial version of your code, and these benefits can often be compounded by using the right parallel paradigm to further speed up the execution of your code.
Key Takeaways
The main optimisation strategies we’ve covered include:
- Memory Management: Understanding stack vs heap allocation and minimizing heap allocations
- Cache Efficiency: Writing cache-friendly code by respecting memory layout and locality
- Reducing Allocations: Preallocating arrays instead of growing them dynamically
- Type Stability: Ensuring the compiler can infer types at compile time
- Vectorisation: Using broadcasting and SIMD operations effectively
- Performance Annotations: Using
@inbounds
,@fastmath
, and other compiler hints when appropriate
Remember that premature optimisation should be avoided - always profile your code first to identify actual bottlenecks before applying these techniques. The tools we covered in theMeasuring Performance chapter are essential for this process.