# Why is dot plus faster?

**URL:** <https://discourse.julialang.org/t/why-is-dot-plus-faster/81830>\
**Category:** Performance\
**Tags:** question\
**Created:** [May 28, 2022, 11:24am UTC](https://discourse.julialang.org/t/why-is-dot-plus-faster/81830 "2022-05-28T11:24:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thisismygitrepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thisismygitrepo/32/28593_2.png) [@thisismygitrepo](https://discourse.julialang.org/u/thisismygitrepo)\
**Post date:** [May 28, 2022, 11:24am UTC](https://discourse.julialang.org/t/why-is-dot-plus-faster/81830/1 "2022-05-28T11:24:22Z")

</div>

I’m experimenting with differnt expression and I can’t explain why the dot + is faster than just + in the simple case of matrix addition.

```julia
using BenchmarkTools 
A = rand(100, 100) 
B = rand(100, 100) 

function no_dot(A, B)
    return [A+B for i in 1:10]
end

function with_dot(A, B)
    return [A.+B for i in 1:10]
end

@btime no_dot(A,B)
@btime with_dot(A, B)

```

The second is 10% faster.

---

<div class="post-metadata">

**Author:** ![Karthik-d-k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karthik-d-k/32/35438_2.png) [@Karthik-d-k](https://discourse.julialang.org/u/Karthik-d-k)\
**Post date:** [May 28, 2022, 11:32am UTC](https://discourse.julialang.org/t/why-is-dot-plus-faster/81830/2 "2022-05-28T11:32:56Z")

</div>

For me, i get the same performance and also i checked @code\_native →   
both functions generates the same code…!!

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [May 28, 2022, 11:32am UTC](https://discourse.julialang.org/t/why-is-dot-plus-faster/81830/3 "2022-05-28T11:32:58Z")

</div>

Is it?

```julia
julia> @benchmark no_dot(A,B)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max): 61.900 μs … 6.048 ms ┊ GC (min … max): 0.00% … 93.50%
 Time (median): 78.600 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 128.704 μs ± 181.175 μs ┊ GC (mean ± σ): 11.31% ± 9.80%

  ██▄▄▂▁ ▃▃▂▂▁ ▂
  ████████▇▇▇▇▆▆▅▆▆▆▆▅▇███████▇▇▆▆▅▅▅▃▄▄▃▁▃▁▁▁▁▃▃▃▁▃▅▅▄▆▅▆▆▅▆▆▅ █
  61.9 μs Histogram: log(frequency) by time 1 ms <

 Memory estimate: 781.84 KiB, allocs estimate: 21.

julia> @benchmark with_dot(A,B)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max): 62.500 μs … 5.696 ms ┊ GC (min … max): 0.00% … 93.94%
 Time (median): 78.600 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 108.850 μs ± 146.224 μs ┊ GC (mean ± σ): 11.28% ± 9.42%

  ▇█▄▃▂▁ ▁▁ ▂
  ███████▇█▆▇▇▆▇▅▆▅▅▆▅▅▅▆▇██▇▇▅▆▅▅▅▁▄▅▅▅▄▄▄▁▄▁▄▃▄▃▃▄▅▅▆▅▅▅▅▆▅▅▅ █
  62.5 μs Histogram: log(frequency) by time 900 μs <

 Memory estimate: 781.84 KiB, allocs estimate: 21.

```

Shouldn’t be any significant difference, because `A+B` broadcasts internally.

---

<div class="post-metadata">

**Author:** ![thisismygitrepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thisismygitrepo/32/28593_2.png) [@thisismygitrepo](https://discourse.julialang.org/u/thisismygitrepo)\
**Post date:** [May 28, 2022, 11:39am UTC](https://discourse.julialang.org/t/why-is-dot-plus-faster/81830/4 "2022-05-28T11:39:43Z")

</div>

Thanks for the check, perhaps I should remove the post cause it was confusing even for me.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [May 28, 2022, 12:10pm UTC](https://discourse.julialang.org/t/why-is-dot-plus-faster/81830/5 "2022-05-28T12:10:25Z")

</div>

Just for you, I suggested (because it’s a tricky situation, 10% can easily be noise, loaded computer):  
[https://github.com/JuliaCI/BenchmarkTools.jl/issues/283](https://github.com/JuliaCI/BenchmarkTools.jl/issues/283)

Ok, I didn’t for you specifically, but looking up issues under my name I didn’t find it at first, so was actually going to do it for you…, could have sworn I had suggested in the past, which I actually had. Not sure why the search failed the first time.

I did notice the interesting: “Feature suggestion: @ballocs” for some reason I read that as bollocks (would it read the same in English?).

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 28, 2022, 12:24pm UTC](https://discourse.julialang.org/t/why-is-dot-plus-faster/81830/6 "2022-05-28T12:24:00Z")

</div>

[https://juliaci.github.io/BenchmarkTools.jl/dev/manual/#TrialRatio-and-TrialJudgement](https://juliaci.github.io/BenchmarkTools.jl/dev/manual/#TrialRatio-and-TrialJudgement)

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [May 28, 2022, 12:57pm UTC](https://discourse.julialang.org/t/why-is-dot-plus-faster/81830/7 "2022-05-28T12:57:52Z")

</div>

Ok, I think you’re saying already possible (just complicated). See link, but about implementing something better/user-friendly see the open issue. Let’s not discuss new implementation here, to not split the discussion. [Would be ok to post here how do do this exactly, with as-is tools.]
