# Something faster than for loops

**URL:** <https://discourse.julialang.org/t/something-faster-than-for-loops/24001>\
**Category:** General Usage\
**Created:** [May 8, 2019, 4:45pm UTC](https://discourse.julialang.org/t/something-faster-than-for-loops/24001 "2019-05-08T16:45:05Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [May 8, 2019, 8:35pm UTC](https://discourse.julialang.org/t/something-faster-than-for-loops/24001/22 "2019-05-08T20:35:13Z")

</div>

They aren’t doing the same thing. The first one isn’t doing matrix multiplication.

---

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [May 8, 2019, 8:36pm UTC](https://discourse.julialang.org/t/something-faster-than-for-loops/24001/23 "2019-05-08T20:36:52Z")

</div>

> [@Aquaman](#):
>
> function f(n,p,m) A=rand(n,p) B=rand(p,m) C=zeros(Float64,n,m) for i in 1:n for j in 1:m C[i,j]=0.0 C[i,j] = C[i,j]+A[i,j]\*B[i,j] end end end

you are fully correct! I should add new benchmark results

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [May 8, 2019, 8:38pm UTC](https://discourse.julialang.org/t/something-faster-than-for-loops/24001/24 "2019-05-08T20:38:20Z")

</div>

And you are measuring the compile time and also include the random number generation inside your function. I’d rather focus on what you want to compare, without that overhead.

I don’t have the feeling that you have read the [performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/index.html) which was suggested by rdeits in the very first reply. I think you should do that 😉

---

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [May 8, 2019, 8:46pm UTC](https://discourse.julialang.org/t/something-faster-than-for-loops/24001/25 "2019-05-08T20:46:31Z")

</div>

```julia
using LinearAlgebra

function f2(n,p,m)

          A=rand(n,p)
          B=rand(p,m)
          C=zeros(Float64,n,m)
          mul!(C, A, B)

 end

 function f1(n,p,m)

           A=rand(n,p)
           B=rand(p,m)
           C=zeros(Float64,n,m)
           for i in 1:n
              for j in 1:m
                 C[i,j]=0.0
               for k in 1:p

                    C[i,j] = C[i,j]+A[i,k]*B[k,j]
                end

               end
            end
        end

@time f1(2000,2000,2000)

@time f2(2000,2000,2000)

```

Time.

> 148.219121 seconds (28.52 k allocations: 92.924 MiB, 0.06% gc time)  
> 0.360905 seconds (6.07 k allocations: 91.819 MiB, 30.07% gc time)

very helpful answer!

---

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [May 8, 2019, 8:52pm UTC](https://discourse.julialang.org/t/something-faster-than-for-loops/24001/26 "2019-05-08T20:52:39Z")

</div>

Sure, multiplication is great useful.🙂

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [May 8, 2019, 9:01pm UTC](https://discourse.julialang.org/t/something-faster-than-for-loops/24001/27 "2019-05-08T21:01:41Z")

</div>

You still measure the compilation time…

On my Xeon machine, the first run is:

```julia
@time f1(2000,2000,2000)
@time f2(2000,2000,2000)

 16.328089 seconds (28.54 k allocations: 92.924 MiB, 0.03% gc time)
  0.371111 seconds (827.86 k allocations: 131.153 MiB, 3.62% gc time)

```

and the second

```julia
@time f1(2000,2000,2000)
@time f2(2000,2000,2000)

 16.271583 seconds (10 allocations: 91.553 MiB, 0.01% gc time)
  0.097989 seconds (10 allocations: 91.553 MiB, 2.01% gc time)

```

again, read the docs 😉

---

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [May 8, 2019, 9:03pm UTC](https://discourse.julialang.org/t/something-faster-than-for-loops/24001/28 "2019-05-08T21:03:45Z")

</div>

```julia
qualitative 

quantitative

```

.🙂

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [May 8, 2019, 9:25pm UTC](https://discourse.julialang.org/t/something-faster-than-for-loops/24001/29 "2019-05-08T21:25:47Z")

</div>

These kinds of responses are likely to get you less help the next time you have a question. Just my two cents.

[Previous page](https://discourse.julialang.org/t/something-faster-than-for-loops/24001.md?page=1)
