# Product of three matrices fast

**URL:** https://discourse.julialang.org/t/product-of-three-matrices-fast/15339
**Category:** Performance
**Created:** [September 22, 2018, 2:55pm UTC](https://discourse.julialang.org/t/product-of-three-matrices-fast/15339 "2018-09-22T14:55:19Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jarl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jarl/32/4365_2.png) [@jarl](https://discourse.julialang.org/u/jarl)
#### Post date: [September 22, 2018, 2:55pm UTC](https://discourse.julialang.org/t/product-of-three-matrices-fast/15339/1 "2018-09-22T14:55:19Z")

</div>

I want to multiply three matrices together as efficiently as possible.

We all know that if you have tall and skinny or fat and short matrices, the order of operations when multiplying three matrices matter a lot for efficiency.

Example:

```julia
n1=5000; n2=10; n3=5000; n4=10
A=randn(n1,n2); B=randn(n2,n3); C=randn(n3,n4)
@btime A*(B*C);  

```

218.357 μs (3 allocations: 391.58 KiB)

```julia
@btime (A*B)*C;  

```

105.064 ms (4 allocations: 191.12 MiB)

Is there a way in julia to automatically select the best multiplication order?

I think I know how to create a flop-count based decision. Ideally it would also take the practical performance on the specific computer it is running into account.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 22, 2018, 2:56pm UTC](https://discourse.julialang.org/t/product-of-three-matrices-fast/15339/2 "2018-09-22T14:56:37Z")

</div>

> **[GitHub - AustinPrivett/MatrixChainMultiply.jl: Find the fastest way to...](https://github.com/AustinPrivett/MatrixChainMultiply.jl)**
>
> Find the fastest way to multiply a chain of matrices and do it. - GitHub - AustinPrivett/MatrixChainMultiply.jl: Find the fastest way to multiply a chain of matrices and do it.

---

<div class="post-metadata">

### Author: ![jarl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jarl/32/4365_2.png) [@jarl](https://discourse.julialang.org/u/jarl)
#### Post date: [September 22, 2018, 2:57pm UTC](https://discourse.julialang.org/t/product-of-three-matrices-fast/15339/3 "2018-09-22T14:57:27Z")

</div>

Awesome!

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 22, 2018, 2:58pm UTC](https://discourse.julialang.org/t/product-of-three-matrices-fast/15339/4 "2018-09-22T14:58:05Z")

</div>

Though, It seems it was a while since it was updated so it might not work out of the box with newest julia.

---

<div class="post-metadata">

### Author: ![jarl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jarl/32/4365_2.png) [@jarl](https://discourse.julialang.org/u/jarl)
#### Post date: [September 22, 2018, 3:09pm UTC](https://discourse.julialang.org/t/product-of-three-matrices-fast/15339/5 "2018-09-22T15:09:56Z")

</div>

The alternative to not specify the order and just `A*B*C` or equivalently(?) `*(A,B,C)` already seems to do some smart things which may be sufficient for me. Is this documented somewhere?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 22, 2018, 3:11pm UTC](https://discourse.julialang.org/t/product-of-three-matrices-fast/15339/6 "2018-09-22T15:11:09Z")

</div>

It does not do anything smart.
