# Btime for loading @tensor inside loop

**URL:** https://discourse.julialang.org/t/btime-for-loading-tensor-inside-loop/100613
**Category:** Numerics
**Created:** [June 20, 2023, 3:17pm UTC](https://discourse.julialang.org/t/btime-for-loading-tensor-inside-loop/100613 "2023-06-20T15:17:16Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Nellatur](https://avatars.discourse-cdn.com/v4/letter/n/76d3ee/32.png) [@Nellatur](https://discourse.julialang.org/u/Nellatur)
#### Post date: [June 20, 2023, 3:17pm UTC](https://discourse.julialang.org/t/btime-for-loading-tensor-inside-loop/100613/1 "2023-06-20T15:17:16Z")

</div>

Hi, for the following code

```julia
using LinearAlgebra
using BenchmarkTools
using TensorOperations

# Initialization
A = rand(100, 100)
B = rand(100, 100)

B_3d = rand(100, 100, 100)

n=100
C_new = zeros(100, 100, 100)
@btime @tensor C_new[a,c,d] := A[a,b] *B_3d[b,c,d]

@btime begin
for d = 1:n
    C_new[:,:,d] = LinearAlgebra.BLAS.gemm('N', 'N', A, B_3d[:,:,d])
end
end

@btime begin
for d = 1:n
   @tensor C_new[a,c,d] := A[a,b] *B_3d[b,c,d]
end
end

```

I got

```julia
  6.801 ms (3 allocations: 7.63 MiB)
  10.129 ms (701 allocations: 15.27 MiB)
  1.030 s (401 allocations: 762.95 MiB)

```

what is the reason for the third timing, that I tried to load @tensor as  
a matrix multiplication, A[a,b] \*B\_3d[b,c,d] ← for fixed d, is much slower than the first two?

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [June 20, 2023, 4:23pm UTC](https://discourse.julialang.org/t/btime-for-loading-tensor-inside-loop/100613/2 "2023-06-20T16:23:37Z")

</div>

Try

```julia
@btime begin
for d = 1:n
   Cview = @view C_new[:,:,d]
   Bview = @view B_3d[:,:,d]
   @tensor Cview[a,c] := A[a,b] *Bview[b,c]
end
end

```

The optimization isn’t the `@view`, I think you aren’t slicing at all, but doing

```julia
@btime begin
for _ = 1:n
   @tensor C_new[a,c,d] := A[a,b] *B_3d[b,c,d]
end
end

```

I have not tested your code to confirm it.

---

<div class="post-metadata">

### Author: ![Nellatur](https://avatars.discourse-cdn.com/v4/letter/n/76d3ee/32.png) [@Nellatur](https://discourse.julialang.org/u/Nellatur)
#### Post date: [June 21, 2023, 1:07am UTC](https://discourse.julialang.org/t/btime-for-loading-tensor-inside-loop/100613/3 "2023-06-21T01:07:28Z")

</div>

Thanks. I got

```julia
ERROR: LoadError: syntax: unsupported assignment operator ":="

```

at the first line of

```julia
@btime begin
for d = 1:n
   Cview = @view C_new[a,c,d]
   Bview = @view B_3d[b,c,d]
   @tensor Cview := A[a,b] *Bview
end
end

```

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [June 21, 2023, 1:17am UTC](https://discourse.julialang.org/t/btime-for-loading-tensor-inside-loop/100613/4 "2023-06-21T01:17:13Z")

</div>

Sorry, I fixed my previous comment.

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [June 21, 2023, 1:31am UTC](https://discourse.julialang.org/t/btime-for-loading-tensor-inside-loop/100613/5 "2023-06-21T01:31:17Z")

</div>

Benchmarks:

```julia
julia> @btime @tensor $C_new[a,c,d] = $A[a,b] * $B_3d[b,c,d];
  936.132 μs (0 allocations: 0 bytes)

julia> f!(C_new, A, B_3d, n) = begin
       for d = 1:n
          LinearAlgebra.BLAS.gemm!('N', 'N', 1.0, A, @view(B_3d[:,:,d]), 0.0, @view(C_new[:,:,d]))
       end
       end; @btime f!($C_new, $A, $B_3d, $n)
  1.417 ms (0 allocations: 0 bytes)

julia> g!(C_new, A, B_3d, n) = begin
       for d = 1:n
          Cview = @view C_new[:,:,d]
          Bview = @view B_3d[:,:,d]
          @tensor Cview[a,c] = A[a,b] * Bview[b,c]
       end
       end; @btime g!($C_new, $A, $B_3d, $n)
  1.454 ms (100 allocations: 6.25 KiB)

julia> h!(C_new, A, B_3d, n) = begin
       for d = 1:n
          Cview = @view C_new[:,:,d]
          Bview = @view B_3d[:,:,d]
          mul!(Cview, A, Bview)
       end
       end; @btime h!($C_new, $A, $B_3d, $n)
  1.414 ms (0 allocations: 0 bytes)

```

Note that I’m using `=` to be in place and avoid allocations, but `g!` still has them for some reason.
