# loops\_Time

**URL:** <https://discourse.julialang.org/t/loops-time/97808>\
**Category:** New to Julia\
**Created:** [April 23, 2023, 7:03am UTC](https://discourse.julialang.org/t/loops-time/97808 "2023-04-23T07:03:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![VISHWARATH\_T](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vishwarath_t/32/49212_2.png) [@VISHWARATH\_T](https://discourse.julialang.org/u/VISHWARATH_T)\
**Post date:** [April 23, 2023, 7:03am UTC](https://discourse.julialang.org/t/loops-time/97808/1 "2023-04-23T07:03:24Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/1/8/1893787e8b737c90fefead26c88b784400a56a77.png)  
Hello,

The time taken to compile the code is less for the nested for loop though I thought the Julia way is more efficient. Can anyone clarify?

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [April 23, 2023, 7:23am UTC](https://discourse.julialang.org/t/loops-time/97808/2 "2023-04-23T07:23:55Z")

</div>

In general, Julia only compiles code put into functions. I recommend reading the [performance tips section of the manual](https://docs.julialang.org/en/v1/manual/performance-tips/).

If you put it in a function, you get results like below (note that the bottleneck here probably is allocating the arrays)

```julia
julia> function f()
           m, n = 5, 5
           A = fill(0, (m, n))
           for j in 1:n
               for i in 1:m
                   A[i, j] = i + j
               end
           end
           A
       end
f (generic function with 1 method)

julia> using BenchmarkTools

julia> f()
5×5 Matrix{Int64}:
 2 3 4 5 6
 3 4 5 6 7
 4 5 6 7 8
 5 6 7 8 9
 6 7 8 9 10

julia> @benchmark f()
BenchmarkTools.Trial: 10000 samples with 968 evaluations.
 Range (min … max): 74.483 ns … 822.128 ns ┊ GC (min … max): 0.00% … 81.69%
 Time (median): 78.448 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 84.993 ns ± 51.897 ns ┊ GC (mean ± σ): 3.91% ± 5.94%

```
