# What is the fastest method to shift matrix?

**URL:** <https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 3, 2022, 7:33pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261 "2022-08-03T19:33:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [August 3, 2022, 7:33pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/1 "2022-08-03T19:33:10Z")

</div>

I want to shift a sparse matrix (`A`) one digit to left with adding zeros in the first column at the right. For that I am multiplying the matrix with `Rotate`. However, when the matrix is big in size and diversity of values, it turn on to be slow. Is there a function in Julia to do that more efficiently?

```julia
Rotate=sparse([zeros(1,33); Matrix(1.0I, 33-1, 33-1) zeros(33-1,1)])

A=[-3 -4 -4 -4 -4 -3 -3 -3 -4 -4 -4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
-3782 -3828 -3873 -3919 -3964 -4008 -4052 -4096 -4140 -4184 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 4000 3966 3932 3898 3863 3828 3792 3756 3719 3682 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
    1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  677 667 658 649 639 630 620 610 601 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  960 967 974 980 987 993 1000 1006 1012 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  606 604 603 601 599 598 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  107 113 119 124 130 135 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
    1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 -784 -783 -782 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  -65 -73 -80 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  687 674 661 648 635 621 608 595 581 568 555 541 527 514 500 486 473 459 445 431 417 403 0 0 0 0 0 0 0 0 0 0 0;
 1352 1359 1366 1372 1379 1385 1391 1397 1403 1409 1415 1420 1426 1431 1436 1441 1446 1450 1455 1459 1464 1468 0 0 0 0 0 0 0 0 0 0 0;
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  588 569 550 531 512 493 474 455 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 1980 1986 1992 1997 2003 2008 2013 2018 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
   -1 -1 -1 -2 -2 -2 -2 -2 -2 -2 -2 -2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 1081 1079 1077 1075 1073 1071 1068 1066 1064 1061 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  223 234 244 254 265 275 285 295 306 316 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
   -1 -1 -1 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 1428 1420 1413 1405 1397 1390 1382 1374 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  742 756 769 783 796 810 823 837 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
    1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 1867 1844 1821 1798 1774 1751 1727 1703 1679 1654 1630 1605 1581 1556 1531 1506 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 2458 2476 2493 2511 2528 2545 2562 2579 2595 2611 2627 2643 2658 2674 2688 2703 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
    0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0;
 2262 2235 2208 2180 2152 2124 2096 2067 2039 2010 1981 1952 1922 1893 1863 1833 1803 1773 1743 1712 0 0 0 0 0 0 0 0 0 0 0 0 0;
 2918 2939 2961 2982 3003 3023 3044 3064 3083 3103 3122 3141 3160 3178 3196 3214 3232 3249 3266 3283 0 0 0 0 0 0 0 0 0 0 0 0 0;
    0 0 -1 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 1703 1699 1696 1692 1688 1684 1680 1676 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
  313 330 346 363 379 395 411 428 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
    1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 -174 -171 -168 -165 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 -343 -345 -346 -347 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
    4 4 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 -413 -440 -468 -495 -522 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 2855 2852 2849 2845 2841 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
   -4 -4 -4 -5 -5 -5 -5 -5 -5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 1313 1293 1273 1253 1233 1212 1191 1171 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 2396 2408 2420 2431 2443 2454 2465 2476 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
    1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
-1564 -1557 -1551 -1544 -1537 -1530 -1523 -1516 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
julia> A*Rotate

```

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [August 3, 2022, 8:01pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/2 "2022-08-03T20:01:15Z")

</div>

Would something like this work for you? Obviously could be written smarter than this, but the point is to use `SparseArrays.permute`:

```julia
B = permute(A, 1:6, vcat(2:5,1))
fill!(view(B, :, size(B,2)), zero(eltype(B)))
dropzeros!(B)

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [August 3, 2022, 8:14pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/3 "2022-08-03T20:14:16Z")

</div>

Thanks for your reply.

> [@cgeoga](#):
>
> Obviously could be written smarter than this

How?

I have benchedmark but it seems not effecient:

```julia
julia> @btime A*Rotate
  266.172 ns (5 allocations: 512 bytes)
julia> @btime dropzeros!(fill!(view(permute(A, 1:6, vcat(2:5,1)), :, size(permute(A, 1:6, vcat(2:5,1)),2)), zero(eltype(permute(A, 1:6, vcat(2:5,1))))))
  9.000 μs (140 allocations: 5.88 KiB)

```

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [August 3, 2022, 8:57pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/4 "2022-08-03T20:57:24Z")

</div>

You should put the lines of code in a function before using @btime. The results don’t make much sense otherwise.

> However, when the matrix is big in size and diversity of values, it turn on to be slow.

Can you be more specific and specify the dimension/density you are working with and update your MWE with it so that we can better benchmark it?

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [August 3, 2022, 9:17pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/5 "2022-08-03T21:17:35Z")

</div>

> [@acxz](#):
>
> Can you be more specific and specify the dimension/density you are working with and update your MWE with it so that we can better benchmark it?

I have updated my MWE.

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [August 3, 2022, 9:48pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/6 "2022-08-03T21:48:03Z")

</div>

As @acxz mentions, that’s not the most correct way to benchmark. But even with more careful benchmarking, you’re right—it is slower. I just assumed that that function would be doing something clever enough to beat the matrix-matrix multiply, but clearly that’s not the case. Sorry for suggesting that without actually benchmarking.

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [August 3, 2022, 9:49pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/7 "2022-08-03T21:49:51Z")

</div>

I think there is hope with your solution!

That dropzeros is probably quite expensive. Maybe there is a way without using dropzeros.

So looking at the source code of SparseArrays we have the following method: dropstored!

> <https://github.com/JuliaLang/julia/blob/742b9abb4dd4621b667ec5bb3434b8b3602f96fd/stdlib/SparseArrays/src/sparsematrix.jl#L3166>

Doing the following might be faster than the just multiplying:

```julia
    dropstored!(A, 1:size(A, 1), 1)
    permute!(A, 1:size(A, 1), vcat(2:size(A, 2), 1))

```

The problem is you can’t use dropstored! atm since its not in the latest released SparseArrays version afaik

The smarter thing to do would be to only dropzeros! everyso often when multiplication starts becoming slow. You just want to drop the extra values you keep around, but drop them every operation will be too costly.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 3, 2022, 9:58pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/8 "2022-08-03T21:58:02Z")

</div>

why don’t you try this?

```julia
using ShiftedArrays
ShiftedArray(A,(0,-1),default=0)

```

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [August 3, 2022, 10:21pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/9 "2022-08-03T22:21:38Z")

</div>

In that case we’ll lose the sparsity nature of these matrices. Doing operations on sparse matrices will be much slower using ShiftedArrays than with SparseArrays.

---

<div class="post-metadata">

**Author:** ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)\
**Post date:** [August 4, 2022, 8:26am UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/10 "2022-08-04T08:26:51Z")

</div>

Just to check, is `A` of type `SparseMatrixCSC{T, Int}`? As your MWE, it is currently a `Matrix`.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 4, 2022, 8:41am UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/11 "2022-08-04T08:41:13Z")

</div>

I have no way to test, but does such a thing have a chance to be faster than multiplying A \* Rotate?

```julia
r,c,v=findnz(A)
c.-=1
n=count(==(0),c)+1 # or n=findfirst(!=(0),c)
sparse(r[n:end], c[n:end],v[n:end], size(A)...)

```

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [August 4, 2022, 12:23pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/12 "2022-08-04T12:23:04Z")

</div>

Just tested it, im afraid it is slower than both of the previous methods discussed.

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [August 4, 2022, 12:44pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/13 "2022-08-04T12:44:05Z")

</div>

Here are the results for the methods discussed. Tested for larger matrix sizes and the permute method is noticeably faster, and for the dimensions and density (extrapolated from the dense matrix given) the permute method is still a slightly bit better.  
I’d just test the permute solution for your workload @Amro and see if the timings are better on your specific hardware/workload.

And to get more speed out of the permute method I would wait for dropstored! to be released in the SparseArrays library as well as dropzeros! not at every multiplication but every say 5 multiplications.

Summary:

```julia
using BenchmarkTools
using LinearAlgebra
using SparseArrays
using Random

function trie()
    row_dim = 44
    col_dim = 33

    A_density = 0.2679063360881543
    rng = MersenneTwister(1)

    A = sprand(rng, row_dim, col_dim, A_density)

    # Matrix mult
    shift = spdiagm(-1 => ones(size(A, 2) - 1))
    A *= shift

    # Permute
    A[:, 1] .= 0
    dropzeros!(A)
    permute!(A, 1:size(A, 1), vcat(2:size(A, 2), 1))

    # New matrix
    rows, cols, vals = findnz(A)
    new_idx = findfirst(!=(1), cols)
    cols .-= 1
    A = sparse(
        rows[new_idx:end],
        cols[new_idx:end],
        vals[new_idx:end],
        size(A)...
    )

end

```

Results in order of method appearing in function. Other methods commented out when code was ran.

```julia
julia> include("shift_sparse.jl")
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max): 21.370 μs … 2.427 ms ┊ GC (min … max): 0.00% … 91.08%
 Time (median): 23.271 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 26.106 μs ± 41.812 μs ┊ GC (mean ± σ): 3.85% ± 2.55%

  ▆█▃▁▁▁ ▁
  ████████▆▅▇▇▇▆▆▅▅▅▅▁▅▅▄▁▅▄▄▆▄▁▃▁▄▃▃▁▃▁▃▁▁▁▁▁▁▁▁▁▁▁▃▃▅▅▆▆▅▅▅ █
  21.4 μs Histogram: log(frequency) by time 99.2 μs <

 Memory estimate: 53.78 KiB, allocs estimate: 37.

julia> include("shift_sparse.jl")
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max): 20.606 μs … 2.020 ms ┊ GC (min … max): 0.00% … 67.20%
 Time (median): 22.429 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 25.574 μs ± 47.173 μs ┊ GC (mean ± σ): 4.06% ± 2.27%

  ▆█▂▁▂▁▁▁▂ ▁
  ████████████▅▅▅▅▃▅▄▄▅▃▄▁▃▃▃▃▄▃▁▁▁▃▁▁▁▁▁▃▁▁▁▁▁▁▁▁▁▁▃▃▅▅▅▆▅▄▆ █
  20.6 μs Histogram: log(frequency) by time 98.9 μs <

 Memory estimate: 34.62 KiB, allocs estimate: 23.

julia> include("shift_sparse.jl")
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max): 29.102 μs … 2.655 ms ┊ GC (min … max): 0.00% … 78.84%
 Time (median): 32.237 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 39.756 μs ± 81.615 μs ┊ GC (mean ± σ): 5.91% ± 2.98%

  ▄█                                                           
  ██▃▃▃▂▃▃▂▂▂▂▂▂▂▁▁▁▂▁▁▁▁▁▁▁▁▂▁▁▁▁▁▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂ ▂
  29.1 μs Histogram: frequency by time 179 μs <

 Memory estimate: 60.55 KiB, allocs estimate: 32.

```

TLDR: use permute method

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [August 4, 2022, 3:13pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/14 "2022-08-04T15:13:51Z")

</div>

It is sparse matrix in my real code. But I could not find a way to copy and paste it here. So, I changed it to dense and paste it.

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [August 4, 2022, 4:05pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/15 "2022-08-04T16:05:43Z")

</div>

You shouldn’t be copy values directly from your code, but rather try to create your MWE from important properties of your code. For example, using the density/dimensions of the matrix you are working with and creating a random sparse matrix with that information. See my example on how to create such a MWE without hardcoding your values but using properties and random values.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [August 4, 2022, 4:15pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/16 "2022-08-04T16:15:01Z")

</div>

Thank you very much for your tips.

> [@acxz](#):
>
> I’d just test the permute solution for your workload @Amro and see if the timings are better on your specific hardware/workload.

> [@acxz](#):
>
> ```julia
> # Permute
> A[:, 1] .= 0
> dropzeros!(A)
> permute!(A, 1:size(A, 1), vcat(2:size(A, 2), 1))
> 
> ```

I have tried the `permute!` with `dropzeros!` but it is slower. Yes, I think I should not apply the `dropzeros!` at each time, but this is a bit annoying.🙂

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [August 4, 2022, 4:15pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/17 "2022-08-04T16:15:46Z")

</div>

Can you post the `@bprofile` output comparison of it? I couldn’t imagine the difference to be that significant if there is one, considering on my PC I get the opposite results.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [August 4, 2022, 4:20pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/18 "2022-08-04T16:20:24Z")

</div>

Ok, I will. Are you using `dropzeros!(A)` at each time step?

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [August 4, 2022, 4:21pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/19 "2022-08-04T16:21:20Z")

</div>

I am only shifting the matrix once as per the MWE. But yea let’s see what the results are for using dropzeros! at each timestep.

I’ll update my tests to try with multiple timesteps as well.

Edit: can you mention how many timesteps you are working with? Based on that you could tune the value of how many times to call dropzeros!

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [August 4, 2022, 4:32pm UTC](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261/20 "2022-08-04T16:32:12Z")

</div>

> [@acxz](#):
>
> @bprofile

time loop simulation 0:25e-6:0.6.

[Next page](https://discourse.julialang.org/t/what-is-the-fastest-method-to-shift-matrix/85261.md?page=2)
