# For Loop optimisation in Julia

**URL:** https://discourse.julialang.org/t/for-loop-optimisation-in-julia/84104
**Category:** New to Julia
**Created:** [July 12, 2022, 1:25pm UTC](https://discourse.julialang.org/t/for-loop-optimisation-in-julia/84104 "2022-07-12T13:25:26Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![lir](https://avatars.discourse-cdn.com/v4/letter/l/edb3f5/32.png) [@lir](https://discourse.julialang.org/u/lir)
#### Post date: [July 12, 2022, 1:25pm UTC](https://discourse.julialang.org/t/for-loop-optimisation-in-julia/84104/1 "2022-07-12T13:25:26Z")

</div>

1. 

I am familiarizing myself with programming languages.

Came across a video “Go vs BUN and Node speed test”

[![](https://global.discourse-cdn.com/julialang/original/3X/c/8/c839c5ccc02feda089b8470152f87c8c111af2ed.jpeg "Go vs BUN and Node speed test") ](https://www.youtube.com/watch?v=Pw6akc9yavg)

Tried to perform the speed comparison betwen Node.js, Bun and Julia.

For some reason For Loop in Julia turned out to be slower then in Node.js and Bun.

1. 

Code for Node.js and Bun:

```julia
console.time("test");

for (var i = 0; i < 1000000; ++i) console.log(i);

console.timeEnd("test");

```

Code for Julia:

```julia
i = 0

@time while i < 1000000
  println(i)
  global i += 1
end

```

(In ~/.profile specified “export JULIA\_NUM\_THREADS=4”.)

1. 

Results:

Node.js

1. 14.409s
2. 14.709s
3. 14.709s

Bun

1. 6.44s
2. 6.75s
3. 6.78s

Julia

1. 19.546201 seconds (9.10 M allocations: 269.540 MiB, 0.19% gc time, 0.14% compilation time)
2. 19.605892 seconds (9.08 M allocations: 268.285 MiB, 0.15% gc time)
3. 20.677104 seconds (9.08 M allocations: 268.285 MiB, 0.49% gc time)

Without “export JULIA\_NUM\_THREADS=4” speed also slow.

1. 

How to improve results in Julia?  
Thanks.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [July 12, 2022, 1:40pm UTC](https://discourse.julialang.org/t/for-loop-optimisation-in-julia/84104/2 "2022-07-12T13:40:00Z")

</div>

Julia is moderately bad at dealing with global variables: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables). The solution is to _not_ use a global variable and put the code inside a function. With

```julia
function main()
    i = 0
    while i < 1000000
        println(i)
        i += 1
    end
    return i
end

@time main()

```

I get

```julia
  8.606004 seconds (8.10 M allocations: 253.779 MiB, 0.43% gc time, 0.28% compilation time)

```

However with Julia v1.8 it’s possible to get non-horribly-slow global variables by type-annotating them. Now, with

```julia
i::Int = 0

@time while i < 1000000
  println(i)
  global i += 1
end

```

I get

```julia
  8.125404 seconds (9.09 M allocations: 261.307 MiB, 0.48% gc time, 0.46% compilation time)

```

And if you remove side effects from the function, like printing to screen, then you get real optimisation:

```julia
julia> function main()
           i = 0
           while i < 1000000
               # println(i)
               i += 1
           end
           return i
       end
main (generic function with 1 method)

julia> @code_llvm main()
; @ REPL[6]:1 within `main`
define i64 @julia_main_225() #0 {
top:
; @ REPL[6]:7 within `main`
  ret i64 1000000
}

julia> @code_native main()
        .text
; ┌ @ REPL[6]:7 within `main`
        movl $1000000, %eax # imm = 0xF4240
        retq
        nopw %cs:(%rax,%rax)
; └

```

The compiler computes the result of this function at compile-time, and it automatically returns a constant.
