# PermutedDimsArray slower than permutedims?

**URL:** https://discourse.julialang.org/t/permuteddimsarray-slower-than-permutedims/46401
**Category:** New to Julia
**Tags:** performance, blas
**Created:** [September 10, 2020, 5:27pm UTC](https://discourse.julialang.org/t/permuteddimsarray-slower-than-permutedims/46401 "2020-09-10T17:27:56Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [September 10, 2020, 7:20pm UTC](https://discourse.julialang.org/t/permuteddimsarray-slower-than-permutedims/46401/4 "2020-09-10T19:20:00Z")

</div>

To do this as one ordinary matrix multiplication, you need to permute as you do. TensorOperations is often quicker at this, by caching, and by having a faster `permutedims` implementation. Although today only marginally:

```julia
julia> @btime batched_mul_m($M, $x);
  200.668 μs (10 allocations: 1.83 MiB)

julia> using TensorOperations
julia> batched_mul_tk3(A,x) = @tensor D[i,l,k] := A[i,j,k] * x[j,l];

julia> @btime batched_mul_tk3($M, $x);
  191.240 μs (177 allocations: 795.13 KiB)

```

This turns out to be a case where avoiding BLAS entirely pays off, as you don’t have to permute, which is actually taking a majority of the time above:

```julia
julia> @btime permutedims($M, (1, 3, 2));
  35.852 μs (2 allocations: 312.58 KiB)

julia> @btime permutedims(reshape($C, (a1, bs, b2)), (1, 3, 2));
  90.409 μs (6 allocations: 781.47 KiB)

julia> using Tullio, LoopVectorization
julia> batched_mul_tk4(A,x) = @tullio D[i,l,k] := A[i,j,k] * x[j,l];

julia> batched_mul_tk4(M,x) ≈ batched_mul_tk3(M,x) ≈ batched_mul_m(M,x)
true

julia> @btime batched_mul_tk4($M, $x);
  62.684 μs (51 allocations: 784.47 KiB)

```

Finally I think this one is also `batched_mul` without permutation (some `PermutedDimsArray`s are actually OK, but not needed here). With the right branch:

```julia
julia> using NNlib # PR#191

julia> @btime batched_mul($M, reshape($x,20,50,1));
  100.661 μs (25 allocations: 784.34 KiB)

```

---

_[View the full topic](https://discourse.julialang.org/t/permuteddimsarray-slower-than-permutedims/46401)._
