As closures are interesting constructs to build functions with state, I am trying to use them to build automatically finite-state machines. I compared the performance of 10000 calls to a closure with a hand-made version of the same code (based on the explanation in the docs: “A closure is simply a callable object with field names corresponding to captured variables.”).
Using the @time macro (Julia v0.6-dev) I get the following results:
Closure: 0.004185 seconds (20.01 k allocations: 312.750 KiB)
Hand-made version: 0.000055 seconds (5 allocations: 192 bytes)
Code is available in gist.
I was wondering what causes this big difference, so I tried
code_warntype and I found out that the generated code is far more complicate than the hand-made version and this can explain the performance difference.
I have also made a not yet published package containing a macro that can generate automatically the hand-made code (I think the name of the package has to change to make it more meaningful;) ).
I prefer however to use the builtin closure functionality but the performance (runtime and memory usage) worries me. If a user macro can generate this more optimal code, the compiler can do the same (perhaps I am underestimating the number of corner cases that have to be handled but I have not yet found anything that can not be done with the macro).