I tried to speed up a Julia function of several levels of loops, but got
unexpected speed relations. The problem is demonstrated with:
~~~
tic()
j = k = 1
while k <= 10^8
j += k & 1
k += 1
end
toc()
tic()
j = k = 1
while true
k > 10^8 && break
j += k & 1
k += 1
end
toc()
print("Press Enter to exit"); readline(); quit()
~~~
I expected the first loop to be faster, but I got consistently results
similar to the following:
elapsed time: 17.159977386 seconds
elapsed time: 15.309415648 seconds
- What is happening?
- For speed critical applications should I use the "while true; cond &&
break" form?
- Is there a variant of the while loop, which is even faster? (I know a
"for" loop is better here, but that above code is a demo of a surprising
slow down.)
(Win7-x64, Julia Version 0.3.0-prerelease+2157)