Both way of writing a while loop should be the same. If you're seeing a
difference, something else is going on. I'm not able to reproduce this:


function f1()
  j = k = 1
  while k <= 10^8
    j += k & 1
    k += 1
  end
  return j
end

function f2()
  j = k = 1
  while true
    k <= 10^8 || break
    j += k & 1
    k += 1
  end
  return j
end

function f3()
  j = k = 1
  while true
    k > 10^8 && break
    j += k & 1
    k += 1
  end
  return j
end

julia> @time f1()
elapsed time: 0.644661304 seconds (64 bytes allocated)
50000001

julia> @time f2()
elapsed time: 0.640951585 seconds (64 bytes allocated)
50000001

julia> @time f3()
elapsed time: 0.639177183 seconds (64 bytes allocated)
50000001


All three functions produce identical native code. Can you send exactly
what your function definitions are, how you're timing them and perhaps the
output of code_native(f1,())?


On Fri, Mar 28, 2014 at 10:48 AM, Laszlo Hars <laszloh...@gmail.com> wrote:

> Thanks, John, for your replies. In my system your code gives reliable
> results, too, if we increase the loop limits to 10^9:
>
> julia> mean(t1s ./ t2s)
> 11.924373323658703
>
> This 12% makes a significant difference in my function of nested loops
> (could add up to a factor of 2 slow down). So, the question remains:
>
> - what is the fastest coding of a while loop?
>

Reply via email to