I don't think this slowness is because of type instability, as I understand the concept. On a 64-bit machine `Int === Int64` is true. Also, the following is also slow and results in lots of allocation:
local i::Int64 as does simply declaring local i However, if one completely leaves off the declaration/initialization of i (and returns nothing), the code is again fast. Also local i::Int64 = 0 results in fast code. Apparently, if the loop variable is to be retained after the loop terminates, then for best performance it must be initialized and not just declared `local` prior to the loop I'm not sure why this is so. --Peter On Wednesday, May 21, 2014 9:38:37 AM UTC-7, [email protected] wrote: > > Hi Hans, > > There was a huge slowdown in your code with : local i::Int. If you change > it to i = 0 (therefore i::Int64), the return type is always Int64. > Therefore, Julia can generate efficient code. > > Use @time to check it. This change makes your code 10 time faster on my > machine. > > > On Wednesday, May 21, 2014 8:41:54 AM UTC+2, Hans W Borchers wrote: >> >> Sorry, but this was only an example of using a loop variable outside the >> loop. >> The real code was not that trivial -- was that not clear from the >> question: "Imagine the following function ..."? >> Then I apologize for poor wording. >> Wanting to know when at what point a loop breaks is a natural request in >> many numerical algorithms. >> >>
