Something faster than for loops

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

you are fully correct! I should add new benchmark results

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 which was suggested by rdeits in the very first reply. I think you should do that :wink:

2 Likes
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!

Sure, multiplication is great useful.:slight_smile:

You still measure the compilation time…

On my Xeon machine, the first run is:

@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

@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 :wink:

1 Like
qualitative 

quantitative

.:slight_smile:

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

6 Likes