# Adding large matrix, why .+ is not the default?

**URL:** https://discourse.julialang.org/t/adding-large-matrix-why-is-not-the-default/44708
**Category:** Performance
**Tags:** question, array
**Created:** [August 10, 2020, 8:40pm UTC](https://discourse.julialang.org/t/adding-large-matrix-why-is-not-the-default/44708 "2020-08-10T20:40:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![raphaelchinchilla](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@raphaelchinchilla](https://discourse.julialang.org/u/raphaelchinchilla)
#### Post date: [August 10, 2020, 8:40pm UTC](https://discourse.julialang.org/t/adding-large-matrix-why-is-not-the-default/44708/1 "2020-08-10T20:40:48Z")

</div>

Take three large matrix A, B, C each with 1000x1000 elements. Here is the result for two benchmarks:

- using broadcasted operator “.+”

```julia
@benchmark A.=A.+B.+C
BenchmarkTools.Trial: 
  memory estimate: 96 bytes
  allocs estimate: 4
  --------------
  minimum time: 1.866 ms (0.00% GC)
  median time: 1.978 ms (0.00% GC)
  mean time: 2.004 ms (0.00% GC)
  maximum time: 2.771 ms (0.00% GC)
  --------------
  samples: 2474
  evals/sample: 1

```

- using the regular operator “+”

```julia
@benchmark A.=A+B+C
BenchmarkTools.Trial: 
  memory estimate: 7.63 MiB
  allocs estimate: 4
  --------------

  minimum time: 5.665 ms (0.00% GC)
  median time: 5.981 ms (0.00% GC)
  mean time: 6.917 ms (12.94% GC)
  maximum time: 35.410 ms (39.02% GC)
  --------------
  samples: 722
  evals/sample: 1

```

Considering that the broadcasted versions is 5 to 16 times faster, why it is not the default? Why do we even need a “+” operator for matrices if the “.+” is faster? Is there any reason or situation where I would have faster/more stable code if I wrote “+” instead of “.+”? Otherwise, It just seems silly that I need to write “.+” (or use a macro) every time I am doing a matrix addition.

---

<div class="post-metadata">

### Author: ![raphaelchinchilla](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@raphaelchinchilla](https://discourse.julialang.org/u/raphaelchinchilla)
#### Post date: [August 10, 2020, 8:51pm UTC](https://discourse.julialang.org/t/adding-large-matrix-why-is-not-the-default/44708/2 "2020-08-10T20:51:54Z")

</div>

As a remark, I have checked that if I do not allocate the result, i.e. I run

```julia
@benchmark A+B+C

```

and

```julia
@benchmark A.+B.+C

```

the runing time is exactly the same, which puzzles me even more about what is happening.

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [August 10, 2020, 8:58pm UTC](https://discourse.julialang.org/t/adding-large-matrix-why-is-not-the-default/44708/3 "2020-08-10T20:58:15Z")

</div>

In the first case, the calculation is broadcasted elementwise, including the step of writing into `A`. In the second case, the entire right side is evaluated, allocating a temporary matrix, which is then written elementwise into `A`. You can tell this is the case based on the allocations. Also note that benchmarking in the global scope is tricky and requires variable interpolation with `$` to return accurate results. In the first case, there should be no allocation at all.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 10, 2020, 9:10pm UTC](https://discourse.julialang.org/t/adding-large-matrix-why-is-not-the-default/44708/4 "2020-08-10T21:10:40Z")

</div>

> [@raphaelchinchilla](#):
>
> Why do we even need a “+” operator for matrices if the “.+” is faster? Is there any reason or situation where I would have faster/more stable code if I wrote “+” instead of “.+”? Otherwise, It just seems silly that I need to write “.+” (or use a macro) every time I am doing a matrix addition.

See [More Dots: Syntactic Loop Fusion in Julia](https://julialang.org/blog/2017/01/moredots/#why_does_julia_need_dots_to_fuse_the_loops)
