Speed up solution system of ODEs

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”.)

3 Likes

Thanks. Btw… if I have made a global variable const — is there a way to “clear” it or release the constancy, or is this only possible if I close down that current Julia session?

I think restarting Julia is the only way but I could easily be wrong.

1 Like

In general you can’t know how big a string is at compile time (Like for example if you are reading it in from a file), and strings can be big… like the entire contents of Moby Dick or the 4th mouse chromosome, or whatever, you wouldn’t allocate that on a stack.

5 Likes