# For-loop in function is faster than global scope?

**URL:** <https://discourse.julialang.org/t/for-loop-in-function-is-faster-than-global-scope/54279>\
**Category:** Performance\
**Tags:** loops, scope\
**Created:** [January 30, 2021, 2:12pm UTC](https://discourse.julialang.org/t/for-loop-in-function-is-faster-than-global-scope/54279 "2021-01-30T14:12:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon74562486](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@anon74562486](https://discourse.julialang.org/u/anon74562486)\
**Post date:** [January 30, 2021, 2:12pm UTC](https://discourse.julialang.org/t/for-loop-in-function-is-faster-than-global-scope/54279/1 "2021-01-30T14:12:57Z")

</div>

Why are loops faster in functions?  
This:

```julia
a = [1:10_000_000;]
b = zeros(Int64, length(a))

function double!(x, y)
    L = length(x)
    for i in 1:L
        @inbounds y[i] = 2x[i]
    end
    nothing
end

@time double!(a, b)

```

is much faster than this:

```julia
@time begin    
    L = length(a)
    for i in 1:L
        @inbounds b[i] = 2a[i]
    end
end

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 30, 2021, 2:24pm UTC](https://discourse.julialang.org/t/for-loop-in-function-is-faster-than-global-scope/54279/2 "2021-01-30T14:24:01Z")

</div>

[https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [January 30, 2021, 2:30pm UTC](https://discourse.julialang.org/t/for-loop-in-function-is-faster-than-global-scope/54279/3 "2021-01-30T14:30:12Z")

</div>

Also, welcome to our community! 🎊

Complementing, the section about [function barriers](https://docs.julialang.org/en/v1/manual/performance-tips/#kernel-functions) is also a little relevant. Basically, when the function is called passing `a` and `b` it is compiled optimized for two arguments of type `Vector{Int64}`, while when you do the loop accessing global variables (the only way to do so without a `function` or `let` body) the code cannot be optimized to assume `a` or `b` are of type `Vector{Int64}` because, as they are `globals` they can have their type changed by other parts of the code that the compiler is not looking at the moment.

---

<div class="post-metadata">

**Author:** ![anon74562486](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@anon74562486](https://discourse.julialang.org/u/anon74562486)\
**Post date:** [January 30, 2021, 2:37pm UTC](https://discourse.julialang.org/t/for-loop-in-function-is-faster-than-global-scope/54279/4 "2021-01-30T14:37:27Z")

</div>

Thanks to everyone
