# Evaluating the "for" condition

**URL:** <https://discourse.julialang.org/t/evaluating-the-for-condition/63050>\
**Category:** Performance\
**Tags:** question\
**Created:** [June 16, 2021, 9:57pm UTC](https://discourse.julialang.org/t/evaluating-the-for-condition/63050 "2021-06-16T21:57:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [June 16, 2021, 9:57pm UTC](https://discourse.julialang.org/t/evaluating-the-for-condition/63050/1 "2021-06-16T21:57:04Z")

</div>

Hello everyone. The following simple example is just to ask the question: will Julia evaluate the length function 5 times? Thank you.

```julia
function test()
	v = [1,2,3,4,5]
	for i = 1:length(v)
		println(v[i])
	end
end

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 16, 2021, 10:00pm UTC](https://discourse.julialang.org/t/evaluating-the-for-condition/63050/2 "2021-06-16T22:00:16Z")

</div>

No

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [June 16, 2021, 10:51pm UTC](https://discourse.julialang.org/t/evaluating-the-for-condition/63050/3 "2021-06-16T22:51:09Z")

</div>

A quick and no-brainer way to check if performance is affected (while not answering your question exactly):

```julia
using BenchmarkTools

v = [1:10_000]

function test1(v)
	for i = 1:length(v)
		println(v[i])
	end
end

@btime test1(v)
120.894 μs (22 allocations: 800 bytes)

function test2(v)
    n = length(v)
	for i = 1:n
		println(v[i])
	end
end

@btime test2(v)
121.065 μs (22 allocations: 800 bytes)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 16, 2021, 11:02pm UTC](https://discourse.julialang.org/t/evaluating-the-for-condition/63050/4 "2021-06-16T23:02:58Z")

</div>

The key thing to understand here is that `1:length(v)` is just an ordinary Julia expression that creates a “range” object storing the endpoints. This object gets evaluated _once_, _before_ the `for` loop starts, and gets re-used on every iteration as needed.

More generally, whenever you do `for x = X` or (equivalently) `for x in X`, for any expression `X`, the expression `X` is evaluated once and then the resulting [iterable object](https://docs.julialang.org/en/v1/base/collections/#lib-collections-iteration) is re-used during the loop.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [June 16, 2021, 11:54pm UTC](https://discourse.julialang.org/t/evaluating-the-for-condition/63050/5 "2021-06-16T23:54:49Z")

</div>

I only knew `@time`, I liked `@btime`, very good!

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [June 17, 2021, 12:02am UTC](https://discourse.julialang.org/t/evaluating-the-for-condition/63050/6 "2021-06-17T00:02:36Z")

</div>

Thank @lmiq, @ptoche and @stevengj.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 17, 2021, 5:10am UTC](https://discourse.julialang.org/t/evaluating-the-for-condition/63050/7 "2021-06-17T05:10:32Z")

</div>

I tend to avoid the combination of `@btime` and `println`. How far do you have to scroll back up to compare the results?

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [June 17, 2021, 5:31am UTC](https://discourse.julialang.org/t/evaluating-the-for-condition/63050/8 "2021-06-17T05:31:50Z")

</div>

In the OP’s code, it’s just something to do inside the loop.

---

<div class="post-metadata">

**Author:** ![healyp](https://avatars.discourse-cdn.com/v4/letter/h/67e7ee/32.png) [@healyp](https://discourse.julialang.org/u/healyp)\
**Post date:** [June 17, 2021, 5:52am UTC](https://discourse.julialang.org/t/evaluating-the-for-condition/63050/9 "2021-06-17T05:52:14Z")

</div>

> [@ptoche](#):
>
> `v = [1:10_000]`

```julia
julia> length(v)
1

```

Though your example is still valid perhaps you meant `v=[i for i in 1:10_000]`?

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [June 17, 2021, 6:56am UTC](https://discourse.julialang.org/t/evaluating-the-for-condition/63050/10 "2021-06-17T06:56:15Z")

</div>

> perhaps you meant `v=[i for i in 1:10_000]` ?

I meant something big that’s all.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 17, 2021, 7:35am UTC](https://discourse.julialang.org/t/evaluating-the-for-condition/63050/11 "2021-06-17T07:35:12Z")

</div>

You can see it explicitely with:

```julia
julia> function test()
           v = [1,2,3,4,5]
           for i = 1:length(v)
               println(v[i])
           end
       end
test (generic function with 1 method)

julia> @code_lowered test()
CodeInfo(
1 ─ v = Base.vect(1, 2, 3, 4, 5)
│ %2 = Main.length(v)
│ %3 = 1:%2
│ @_2 = Base.iterate(%3)
│ %5 = @_2 === nothing
│ %6 = Base.not_int(%5)
└── goto #4 if not %6
2 ┄ %8 = @_2
│ i = Core.getfield(%8, 1)
│ %10 = Core.getfield(%8, 2)
│ %11 = Base.getindex(v, i)
│ Main.println(%11)
│ @_2 = Base.iterate(%3, %10)
│ %14 = @_2 === nothing
│ %15 = Base.not_int(%14)
└── goto #4 if not %15
3 ─ goto #2
4 ┄ return nothing
)

```
