Yikes, that's much harder to read than the original. Making the globals const is a simple change and the fact that this improves the performance so much shows that all those type annotations are not necessary. The only issue with the original code was non-constant globals. We can also change the globals to function arguments and the problem goes away:
julia> @time Ke1(N); elapsed time: 0.237183958 seconds (131200224 bytes allocated, 34.34% gc time) julia> @time Ke2(N); elapsed time: 0.021313495 seconds (400 bytes allocated) The gist has both the const and function-arg versions: https://gist.github.com/StefanKarpinski/e1d7d8804e373cc1de07 On Fri, Dec 12, 2014 at 10:12 AM, Andreas Noack < andreasnoackjen...@gmail.com> wrote: > Stefan, please consider the updated version > > https://gist.github.com/PetrKryslUCSD/7bd14515e1a853275923 > > which has no global variables. > > 2014-12-12 10:07 GMT-05:00 Stefan Karpinski <ste...@karpinski.org>: > > From the original version of the code I see this timing after code gen: >> >> julia> @time Ke1(N); >> elapsed time: 0.251365495 seconds (134400208 bytes allocated, 30.13% gc >> time) >> >> julia> @time Ke2(N); >> elapsed time: 3.923532621 seconds (996800384 bytes allocated, 13.97% gc >> time) >> >> After making all the globals const, I see this: >> >> julia> @time Ke1(N); >> elapsed time: 0.273683599 seconds (131200208 bytes allocated, 28.52% gc >> time) >> >> julia> @time Ke2(N); >> elapsed time: 0.026985097 seconds (384 bytes allocated) >> >> Type annotations everywhere are neither necessary nor recommended. It is >> recommended not to use lots of non-constant globals. >> >> >> On Fri, Dec 12, 2014 at 9:36 AM, Tim Holy <tim.h...@gmail.com> wrote: >> >>> On Thursday, December 11, 2014 08:10:38 PM Petr Krysl wrote: >>> > The moral of this story is: If you can't or won't declare every >>> single >>> > variable, don't do loops. They are likely to be a losing proposition. >>> >>> Just to follow up further, this is not at all the right moral to absorb >>> from >>> this. A better set of morals is: >>> - use code_typed or TypeCheck or Lint to diagnose problems >>> - remember that julia optimizes functions as a unit. Therefore, in a >>> loop with >>> a type issue, one "brute force" solution is to create a separate >>> function just >>> for running that loop. The types will be known when that function gets >>> compiled (even if you don't annotate anything with their types), and so >>> your >>> type problem should evaporate. >>> >>> --Tim >>> >>> >> >