# Contiguous setindex involving an adjoint is slower than non-contiguous for a Matrix

**URL:** https://discourse.julialang.org/t/contiguous-setindex-involving-an-adjoint-is-slower-than-non-contiguous-for-a-matrix/61767
**Category:** General Usage
**Tags:** question, indexing
**Created:** [May 25, 2021, 8:56am UTC](https://discourse.julialang.org/t/contiguous-setindex-involving-an-adjoint-is-slower-than-non-contiguous-for-a-matrix/61767 "2021-05-25T08:56:06Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [May 25, 2021, 8:56am UTC](https://discourse.julialang.org/t/contiguous-setindex-involving-an-adjoint-is-slower-than-non-contiguous-for-a-matrix/61767/1 "2021-05-25T08:56:06Z")

</div>

```julia
julia> a = zeros(500,500);

julia> b = zeros(500,500)';

julia> v = 1:500;

julia> @btime $a[1,:] = $v;
  711.515 ns (0 allocations: 0 bytes)

julia> @btime $b[1,:] = $v;
  808.444 ns (0 allocations: 0 bytes)

julia> @btime $a[:,1] = $v;
  695.041 ns (0 allocations: 0 bytes)

```

I don’t understand why the `setindex!` involving `b` is slower than the non-contiguous `setindex!` involving `a`. If anything, I would have expected indexing `b` along columns to be as performant as indexing `a` along rows.

Interestingly the expected behavior is seen if the assignment is done through broadcasting:

```julia
julia> @btime $a[1,:] .= $v;
  744.464 ns (0 allocations: 0 bytes)

julia> @btime $b[1,:] .= $v;
  649.030 ns (0 allocations: 0 bytes)

julia> @btime $a[:,1] .= $v;
  651.577 ns (0 allocations: 0 bytes)

```

Why is there a difference if the broadcasting is left implicit?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [May 25, 2021, 4:37pm UTC](https://discourse.julialang.org/t/contiguous-setindex-involving-an-adjoint-is-slower-than-non-contiguous-for-a-matrix/61767/2 "2021-05-25T16:37:25Z")

</div>

Yeah. This is really weird. It’s probably an unfortunate generic fallback

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 25, 2021, 4:58pm UTC](https://discourse.julialang.org/t/contiguous-setindex-involving-an-adjoint-is-slower-than-non-contiguous-for-a-matrix/61767/3 "2021-05-25T16:58:48Z")

</div>

> [@jishnub](#):
>
> ```julia
> julia> @btime $b[1,:] = $v;
> 808.444 ns (0 allocations: 0 bytes)
> 
> julia> @btime $a[:,1] = $v;
> 695.041 ns (0 allocations: 0 bytes)
> 
> ```
> 
> I don’t understand why the `setindex!` involving `b` is slower than the non-contiguous `setindex!` involving `a` . If anything, I would have expected indexing `b` along columns to be as performant as indexing `a` along rows.

~~You have it backwards: `b[1,:]` is **non** -contiguous, and `a[:,1]` is contiguous. Remember that Julia arrays are column-major.~~ `b[1,:]` is indeed contiguous because `b` is an adjoint/transpose of a `Matrix`.

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [May 25, 2021, 5:15pm UTC](https://discourse.julialang.org/t/contiguous-setindex-involving-an-adjoint-is-slower-than-non-contiguous-for-a-matrix/61767/4 "2021-05-25T17:15:53Z")

</div>

`b` is the adjoint of a matrix, so the column indices of `b` should be contiguous?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 25, 2021, 6:46pm UTC](https://discourse.julialang.org/t/contiguous-setindex-involving-an-adjoint-is-slower-than-non-contiguous-for-a-matrix/61767/5 "2021-05-25T18:46:44Z")

</div>

Oh, right, I didn’t notice this, sorry.

---

<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: [May 25, 2021, 7:37pm UTC](https://discourse.julialang.org/t/contiguous-setindex-involving-an-adjoint-is-slower-than-non-contiguous-for-a-matrix/61767/6 "2021-05-25T19:37:28Z")

</div>

Might not be crazy to overload something to make this fast, maybe a method of `_setindex!`? [#39467](https://github.com/JuliaLang/julia/pull/39467) did this for the corresponding `view`s of transposed matrices, although times there were a factor 4, while I get about 4/3 for this example.
