Var in for loop specification doubles execution time

question

#1

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.


#2

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


#3

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.