# Strange performance results about function mul! with views

**URL:** <https://discourse.julialang.org/t/strange-performance-results-about-function-mul-with-views/128532>\
**Category:** General Usage\
**Tags:** performance\
**Created:** [April 29, 2025, 3:40pm UTC](https://discourse.julialang.org/t/strange-performance-results-about-function-mul-with-views/128532 "2025-04-29T15:40:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TongTongYee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tongtongyee/32/207244_2.png) [@TongTongYee](https://discourse.julialang.org/u/TongTongYee)\
**Post date:** [April 29, 2025, 3:40pm UTC](https://discourse.julialang.org/t/strange-performance-results-about-function-mul-with-views/128532/1 "2025-04-29T15:40:37Z")

</div>

```julia
aaaa = zeros(1000,1000)
bbbb = ones(1,10000)
cccc = ones(10000,1000)
range = [1]
@btime mul!(@view(aaaa[range,:]),bbbb,cccc);

```

→   
BLAS.get\_num\_threads() → 1  
27.416 ms (3 allocations: 112 bytes)  
BLAS.get\_num\_threads() → 48  
27.514 ms (3 allocations: 112 bytes)

```julia
aaaa = zeros(1000,1000)
bbbb = ones(1,10000)
cccc = ones(10000,1000)
range = 1:1
@btime mul!(@view(aaaa[range,:]),bbbb,cccc);

```

→   
BLAS.get\_num\_threads() → 1  
11.358 ms (5 allocations: 224 bytes)  
BLAS.get\_num\_threads() → 48  
399.946 μs (5 allocations: 224 bytes)

range = 1:1 is much faster than range = [1].

@btime @view(aaaa[range,:].=bbbb\*cccc;  
is not such a case.

Why do the strange results occur?

---

<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:** [April 29, 2025, 4:10pm UTC](https://discourse.julialang.org/t/strange-performance-results-about-function-mul-with-views/128532/2 "2025-04-29T16:10:06Z")

</div>

> [@TongTongYee](#):
>
> range = 1:1 is much faster than range = [1].

Views with a `Vector{Int}` of indices are likely to hit slower paths than views with `UnitRange`, because their contents is less predictable:

```julia
julia> x = rand(5, 3);

julia> view(x, [1,3,2], :) isa StridedArray
false

julia> y = view(x, 1:3, :); y isa StridedArray
true

julia> strides(x) == strides(y) == (1, 5)
true

```

If `1:1` is what you really want, then there are probably further improvements available by using vector types, instead of 1xN matrices:

```julia
julia> @btime mul!(@view($aaaa[[1],:]), $bbbb, $cccc); # slow path above
  24.499 ms (2 allocations: 64 bytes)

julia> @btime mul!(@view($aaaa[1:1,:]), $bbbb, $cccc); # faster path above
  1.480 ms (0 allocations: 0 bytes)

julia> @btime mul!(@view($aaaa[1,:]), $cccc', $(vec(bbbb))); # vector not matrix
  589.125 μs (0 allocations: 0 bytes)

```
