I think that’s a useful summary. I’d emphasise that, other than avoiding global variables, I’d just write the code in the most straightforward way I can think of and then only start optimising if the performance really isn’t good enough. The garbage collector is reasonably good and so I often don’t worry about the allocations that much unless the code is going to be run a lot.
One small point of clarification; it’s not assignments (a = ...
) that cause allocations, it’s the operation on the right-hand side, e.g., a = b + c
where b
and c
are vectors. In this case the result of b + c
is allocated and it’s immaterial whether this is assigned to a variable or used within a larger calculation. As such, don’t be worried about assignments. (Note that in this case, this is where the broadcasting facilities of Julia come in to help reduce allocations - see this nice blog post for more information, particularly the section on “Why vectorised code is not as fast as it could be”.)