Are functions re-read every time they execute or only once on first call?

Hello, I need help on something I could not find my myself.

I have a script foo.m that is calling a function fun that is in a separate file fun.m. The function fun is being called lots of times from foo.m inside a big loop.

I want to know whether fun.m is re-read from disk every time it is called from foo.m, or whether it is read once the first time it is called and then stored in RAM.

I ask because I was editing fun.m in an editor without realizing foo.m was running, and it changed foo.m output in “real time”.

If it is read from disk every time, can I tell Octave not to do so, to speed up run time?

Afaict, functions are only re-read from disc if their modification date changed.

1 Like

This is correct. Octave reads the m-file once from disk, parse it, and then stores it in RAM. Unless the file timestamp has changed–indicating a change in the actual code–Octave uses the cached version in RAM.