# How to implement fast range reads for my custome-defined array?

**URL:** <https://discourse.julialang.org/t/how-to-implement-fast-range-reads-for-my-custome-defined-array/8490>\
**Category:** Performance\
**Tags:** array\
**Created:** [January 20, 2018, 9:20am UTC](https://discourse.julialang.org/t/how-to-implement-fast-range-reads-for-my-custome-defined-array/8490 "2018-01-20T09:20:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)\
**Post date:** [January 20, 2018, 9:20am UTC](https://discourse.julialang.org/t/how-to-implement-fast-range-reads-for-my-custome-defined-array/8490/1 "2018-01-20T09:20:35Z")

</div>

I need to implement my own array type underlying which is still a Vector, but reading/writing to it is much much slower than reading/writing the vanilla Julia array. I can reproduce the problem in this simple program below. Could someone see what I did wrong?

```julia

type MyArray{T, N} <: AbstractArray{T, N}
    values::Vector{T}
    dims::Tuple
    MyArray(dims::NTuple{N, Int64}) = length(dims) > 0 ?
        new(Vector{T}(reduce(*, [dims...])), dims) :
        new(Vector{T}(), dims)
end

function Base.linearindexing(::MyArray)
    return Base.LinearFast()
end

function Base.size(array::MyArray)
    return array.dims
end

function Base.getindex(array::MyArray, i::Int)
    return array.values[i]
end

function Base.setindex!(array::MyArray, v, i::Int)
    array.values[i] = v
end

function Base.similar{T, N}(array::MyArray{T, N}, ::Type{T}, dims::NTuple{N, Int64})
    return MyArray{T, N}(dims)
end

c = MyArray{Float64, 2}((100, 3000))
@time for i = 1:3000
    d = c[:, i]
end

a = rand((100, 3000))
@time for i = 1:3000
    b = a[:, i]
end

```

According to the output of `@time`, it seems my implementation causes a lot more memory allocation than Julia’s. How may I fix it?

> 4.525247 seconds (10.35 M allocations: 228.042 MB, 3.78% gc time)  
> 0.025936 seconds (28.96 k allocations: 3.634 MB)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 20, 2018, 9:51am UTC](https://discourse.julialang.org/t/how-to-implement-fast-range-reads-for-my-custome-defined-array/8490/2 "2018-01-20T09:51:04Z")

</div>

Your `dims::Tuple` field is not concretely typed. Perhaps it should be `::NTuple{N, Int}`?.

Also, it is unikely that you want to have that type be mutable so change to `immutable` (or just `struct` in the new syntax). Note, making the type non mutable does not mean you cannot mutate the values in the `values` field.

---

<div class="post-metadata">

**Author:** ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)\
**Post date:** [January 20, 2018, 10:33pm UTC](https://discourse.julialang.org/t/how-to-implement-fast-range-reads-for-my-custome-defined-array/8490/3 "2018-01-20T22:33:29Z")

</div>

Thanks! This solved my problem!
