# The first for loop execution

**URL:** <https://discourse.julialang.org/t/the-first-for-loop-execution/27841>\
**Category:** Performance\
**Created:** [August 22, 2019, 11:04am UTC](https://discourse.julialang.org/t/the-first-for-loop-execution/27841 "2019-08-22T11:04:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mirsad\_Cosovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mirsad_cosovic/32/9887_2.png) [@Mirsad\_Cosovic](https://discourse.julialang.org/u/Mirsad_Cosovic)\
**Post date:** [August 22, 2019, 11:04am UTC](https://discourse.julialang.org/t/the-first-for-loop-execution/27841/1 "2019-08-22T11:04:16Z")

</div>

Hi,

I wrote a simple test in Julia, I have a large sparse matrix, and I want to iterate throughout non-zero elements, but the first execution of the for loop is slow. The minimal working example is bellow. Did I do something wrong here?

```julia
using SparseArrays

function test()
    H = sprand(30000,70000,0.001)
    cidx = findall(!iszero, H)
    G = spzeros(30000,70000)

    for k = 1:2
        @time begin
        for i in cidx
            G[i] = 1
        end
        end
    end
end

test()

 23.617762 seconds (42 allocations: 66.001 MiB, 0.40% gc time)
  0.028947 seconds

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [August 22, 2019, 11:12am UTC](https://discourse.julialang.org/t/the-first-for-loop-execution/27841/2 "2019-08-22T11:12:28Z")

</div>

You are inserting elements into an _empty_ sparse matrix (`G`) which is extremely inefficient. However, the second loop those entries are already populated so then it is much faster to insert.

---

<div class="post-metadata">

**Author:** ![Mirsad\_Cosovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mirsad_cosovic/32/9887_2.png) [@Mirsad\_Cosovic](https://discourse.julialang.org/u/Mirsad_Cosovic)\
**Post date:** [August 22, 2019, 11:15am UTC](https://discourse.julialang.org/t/the-first-for-loop-execution/27841/3 "2019-08-22T11:15:34Z")

</div>

Oh, nice, if I define G as H, then there is no problem.

Thank you

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [August 22, 2019, 8:51pm UTC](https://discourse.julialang.org/t/the-first-for-loop-execution/27841/4 "2019-08-22T20:51:46Z")

</div>

One way to do this efficiently, using only 2 lines of code, is as follows:

```julia
julia> using SparseArrays, BenchmarkTools

julia> function test()
           H = sprand(30000, 70000, 0.001)
           G = map(x -> !iszero(x) ? 1.0 : 0.0, H)
       end
test (generic function with 1 method)

julia> @btime test();
  79.437 ms (23 allocations: 82.19 MiB)

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [August 23, 2019, 1:14am UTC](https://discourse.julialang.org/t/the-first-for-loop-execution/27841/5 "2019-08-23T01:14:41Z")

</div>

If that is really your situation, you can use `G = sprand(Bool,30000, 70000, 0.001)` and skip all the rest.

---

<div class="post-metadata">

**Author:** ![Mirsad\_Cosovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mirsad_cosovic/32/9887_2.png) [@Mirsad\_Cosovic](https://discourse.julialang.org/u/Mirsad_Cosovic)\
**Post date:** [August 23, 2019, 7:52am UTC](https://discourse.julialang.org/t/the-first-for-loop-execution/27841/6 "2019-08-23T07:52:16Z")

</div>

You’re right, this is not really my situation. I wanted to point out that the first pass is slow. However, fredrikekre gave the explanation.

---

<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:** [August 23, 2019, 8:07am UTC](https://discourse.julialang.org/t/the-first-for-loop-execution/27841/7 "2019-08-23T08:07:48Z")

</div>

> [@tbeason](#):
>
> `G = sprand(Bool,30000, 70000, 0.001)`

I was surprised to see that `sprand(0:1, 30000, 70000, 0.001)` didn’t work, since both `rand(Bool)` and `rand(0:1)` do. Any idea how to achieve this?
