Dot products: a'b or a'*b

If I run

a=randn(10^8)
b=randn(10^8)
@time s=a'b

the time it takes is 0.129 seconds and it says (22.98 k allocations: 1.613 MiB, 31.73% compilation time)
74.59337300358766.

If in the last step I say

@time s=a'*b

I get 0.065235 seconds (2 allocations: 32 bytes)
Can anyone explain a little further or show me where to look better about how a'b and a'*b are different and how is it calling the appropiate methods? Also, what is it saying when it shows 37.73% compilation time. Thanks

Both a'b and a' * b call exactly the same method and generate the exact same code. What’s happening is the first time you run a method Julia compiles it for you. That’s the compilation time showing up. Run them in any order or multiple times, and you’ll only see the slowness (and the compilation part in the timing output) on the first call, regardless of which way you wrote it.

4 Likes

For the reasons mentioned by @mbauman, it’s often advised to use more robust solutions like BenchmarkTools.jl 's @benchmark and @btime macros, which can automatically run the code multiple times to characterize only the actual execution time and generate some statistics to refine the timing estimates.

3 Likes