# Fast way to Implement a Loop

**URL:** <https://discourse.julialang.org/t/fast-way-to-implement-a-loop/18748>\
**Category:** New to Julia\
**Tags:** loops\
**Created:** [December 17, 2018, 3:08pm UTC](https://discourse.julialang.org/t/fast-way-to-implement-a-loop/18748 "2018-12-17T15:08:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmcastro2109](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmcastro2109/32/38427_2.png) [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Post date:** [December 17, 2018, 3:08pm UTC](https://discourse.julialang.org/t/fast-way-to-implement-a-loop/18748/1 "2018-12-17T15:08:44Z")

</div>

Hello, I am beginning to use Julia and I want to ask for help doing something efficiently.

I am trying to compute a summation in Julia using the following loop.

```julia
 for (k_j,kk) = enumerate(k)
       value=0
       for (s_j,ss) = enumerate(s), (z_j,zz) = enumerate(z), (w_j,ww) = enumerate(w)
                    value=value+V₀[w_j,z_j,k_j,s_j]*H[s_i,s_j]*mat[w_j,w_i,z_j,z_i]*G[z_i,z_j]
        end
  end

```

Which is basically calling over specific entries of matrices and adding them up. I’ve tried to make this faster and leaner with a reduce or mapreduce, but haven’t been able to get the code off the ground.

Any suggestion is appreciated, Thanks

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 17, 2018, 3:32pm UTC](https://discourse.julialang.org/t/fast-way-to-implement-a-loop/18748/2 "2018-12-17T15:32:19Z")

</div>

Please provide a self-contained working example, which includes the variants you have tried and the way you are benchmarking them.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Post date:** [December 17, 2018, 6:14pm UTC](https://discourse.julialang.org/t/fast-way-to-implement-a-loop/18748/3 "2018-12-17T18:14:21Z")

</div>

Hi!

There are two things that come to my mind:

1. Wrap your code in a function to allow precompilation and specialization on the types  
(Just pass everything the function needs as arguments - not access global variables)
2. then write `value = 0.0` to make sure that your operation is type stable ( Otherwise it is first initialized as an `Int` and then converted to a `Float64`

If you would like more specific help, you would need to provide a (minimal) working self contained example,  
so that we can just copy your code and it runs.

---

<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:** [December 17, 2018, 9:41pm UTC](https://discourse.julialang.org/t/fast-way-to-implement-a-loop/18748/4 "2018-12-17T21:41:19Z")

</div>

If you post a question in several fora, please provide a link to the other question:

[https://stackoverflow.com/questions/53816866/fast-way-to-implement-a-loop](https://stackoverflow.com/questions/53816866/fast-way-to-implement-a-loop)

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [December 17, 2018, 10:10pm UTC](https://discourse.julialang.org/t/fast-way-to-implement-a-loop/18748/5 "2018-12-17T22:10:30Z")

</div>

Just to be clear here — the “fast” way to write a for loop is to write the three letters `for …`. There’s no magic. There’s no need to contort things to vectorize into a `mapreduce` (or some other) function call. Those functions you call? They just use a Julia `for` loop spelled with exactly the same three letters you write.

Julia’s for loops _are_ fast, particularly if you follow the guidance here: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/)
