# Speed of writing vs. reading elements in an array

**URL:** <https://discourse.julialang.org/t/speed-of-writing-vs-reading-elements-in-an-array/3596>\
**Category:** General Usage\
**Tags:** performance\
**Created:** [May 8, 2017, 2:39pm UTC](https://discourse.julialang.org/t/speed-of-writing-vs-reading-elements-in-an-array/3596 "2017-05-08T14:39:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcovalt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcovalt/32/1722_2.png) [@mcovalt](https://discourse.julialang.org/u/mcovalt)\
**Post date:** [May 8, 2017, 2:39pm UTC](https://discourse.julialang.org/t/speed-of-writing-vs-reading-elements-in-an-array/3596/1 "2017-05-08T14:39:51Z")

</div>

I’m writing a performance critical module. While using `@inbounds`, I can read elements (`getindex`) about 50x faster than I can write elements (`setindex!`) in two arrays of identical type and size. Both arrays are accessed at the same level of irregularity.

Is there anything I can do to speed up writing to an array?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 8, 2017, 2:46pm UTC](https://discourse.julialang.org/t/speed-of-writing-vs-reading-elements-in-an-array/3596/2 "2017-05-08T14:46:42Z")

</div>

Store is usually slower than load though usually not this much slower. It’s almost impossible to give advice without more information about the code.

---

<div class="post-metadata">

**Author:** ![mcovalt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcovalt/32/1722_2.png) [@mcovalt](https://discourse.julialang.org/u/mcovalt)\
**Post date:** [May 8, 2017, 3:25pm UTC](https://discourse.julialang.org/t/speed-of-writing-vs-reading-elements-in-an-array/3596/3 "2017-05-08T15:25:45Z")

</div>

Here is a reduced version of my code:

```julia
function f!{T<:Real}(A::MyImmutableType, x::Vector{T}, y::Vector{T}, z::Vector{T}, nchunks::Integer)
    for chunk = 1:nchunks
        @inbounds for i = 1:128
            row_ind, col_ind = ind2sub(A.dims, A.ints[chunk][i])
            zval = z[A.ints[chunk][i]]
            xval = x[col_ind]
            yval = y[row_ind]
            y[row_ind] = yval + xval*zval
        end
    end
end

function f{T<:Real}(A::MyImmutableType, x::Vector{T}, y::Vector{T}, z::Vector{T}, nchunks::Integer)
    y_scalar = 0.0
    for chunk = 1:nchunks
        @inbounds for i = 1:128
            row_ind, col_ind = ind2sub(A.dims, A.ints[chunk][i])
            zval = z[A.ints[chunk][i]]
            xval = x[col_ind]
            yval = y[row_ind]
            y_scalar += yval + xv*zval
        end
    end
    return y_scalar
end

```

The first function takes ~1.5 seconds to run and the second ~0.03 seconds. Any thoughts?

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [May 8, 2017, 3:35pm UTC](https://discourse.julialang.org/t/speed-of-writing-vs-reading-elements-in-an-array/3596/4 "2017-05-08T15:35:17Z")

</div>

You’ll see some performance regressions because of aliasing issues, but it shouldn’t be this much (I’d expect maybe 4x).

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [May 8, 2017, 3:36pm UTC](https://discourse.julialang.org/t/speed-of-writing-vs-reading-elements-in-an-array/3596/5 "2017-05-08T15:36:28Z")

</div>

Also you’ll probably get a better response if you post a complete runnable example (including the definition of MyImmutableType and the `@time` macros).
