Var in for loop specification doubles execution time

If I do

foo = 1_000_000
for i in 1:foo
  loop_body

the execution takes about twice as long as if I do

for i in 1:1_000_000
  loop_body

Using foo = 1:1_000_000 results in a similar longer execution time.

Are you testing in the REPL / global scope, or in a function?

2 Likes

In a file run from the command line like this:

time julia foo.jl

In the file, the for loop is at the top level (no function).

But your question prompted me to put the for loop in a function. That made the difference and is not unexpected.