# Improve the performance of multiplication of an arbitrary number of matrices

**URL:** https://discourse.julialang.org/t/improve-the-performance-of-multiplication-of-an-arbitrary-number-of-matrices/10835
**Category:** Performance
**Created:** [May 11, 2018, 5:01am UTC](https://discourse.julialang.org/t/improve-the-performance-of-multiplication-of-an-arbitrary-number-of-matrices/10835 "2018-05-11T05:01:49Z")
**Posts on this page:** 1
**Showing post:** 19

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [May 12, 2018, 1:12pm UTC](https://discourse.julialang.org/t/improve-the-performance-of-multiplication-of-an-arbitrary-number-of-matrices/10835/19 "2018-05-12T13:12:11Z")

</div>

Guys,

I found something interesting, can anyone help me? I really can’t explain.

Take a look at the following benchmark:

```julia
D1 = @SMatrix rand(3,3)
D2 = @SMatrix rand(3,3)

@inline function compose_rotation(D1::SMatrix{3,3}, D2::SMatrix{3,3}, Ds::SMatrix{3,3}...)
    result = D2*D1

    for Di in Ds
        result = Di*result
    end

    result
end

test() = D2*D1*D2*D1*D1*D2*D1*D2*D1

@btime test()
@btime compose_rotation(D1,D2,D1,D2,D1,D1,D2,D1,D2)

```

I am getting:

```julia
@btime test()
  614.213 ns (14 allocations: 1.45 KiB)
3×3 StaticArrays.SArray{Tuple{3,3},Float64,2,9}:
 31.6736 6.31623 21.519
 19.4169 3.87201 13.1921
 25.5171 5.08857 17.3361

@btime compose_rotation(D1,D2,D1,D2,D1,D1,D2,D1,D2)
  117.351 ns (1 allocation: 80 bytes)
3×3 StaticArrays.SArray{Tuple{3,3},Float64,2,9}:
 31.6736 6.31623 21.519
 19.4169 3.87201 13.1921
 25.5171 5.08857 17.3361

```

Why the multiplication using the function is 6x faster than the explicit multiplication?

---

_[View the full topic](https://discourse.julialang.org/t/improve-the-performance-of-multiplication-of-an-arbitrary-number-of-matrices/10835)._
