# Strange behavior with array calls

**URL:** <https://discourse.julialang.org/t/strange-behavior-with-array-calls/19200>\
**Category:** General Usage\
**Tags:** question, performance\
**Created:** [January 2, 2019, 4:38pm UTC](https://discourse.julialang.org/t/strange-behavior-with-array-calls/19200 "2019-01-02T16:38:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 2, 2019, 4:38pm UTC](https://discourse.julialang.org/t/strange-behavior-with-array-calls/19200/1 "2019-01-02T16:38:45Z")

</div>

Why is `getindex(g,x)` slower than `g[x]`?

```nohighlight
julia> g = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> f(g,x) = g[x]

julia> @btime f($g,2)
  0.025 ns (0 allocations: 0 bytes)
2

julia> @btime getindex($g,2)
  1.813 ns (0 allocations: 0 bytes)
2

```

---

<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 2, 2019, 4:41pm UTC](https://discourse.julialang.org/t/strange-behavior-with-array-calls/19200/2 "2019-01-02T16:41:39Z")

</div>

A run time of sub nanosecond almost always mean that there was some compiler optimization that made the whole benchmarking loop get optimized away. Also, locally I get

```julia
julia> @btime f($g,2)
  1.542 ns (0 allocations: 0 bytes)
2

julia> @btime getindex($g,2)
  1.541 ns (0 allocations: 0 bytes)
2

```

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 2, 2019, 4:43pm UTC](https://discourse.julialang.org/t/strange-behavior-with-array-calls/19200/3 "2019-01-02T16:43:55Z")

</div>

> [@kristoffer.carlsson](#):
>
> A run time of sub nanosecond almost always mean that there was some compiler optimization that made the whole benchmarking loop get optimized away. Also, locally I get

Could it be that the difference in optimizations between our local computers is due to differences in software installed by the operating system or how Julia was compiled for the OS? Mine is running arch linux

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 2, 2019, 4:50pm UTC](https://discourse.julialang.org/t/strange-behavior-with-array-calls/19200/4 "2019-01-02T16:50:28Z")

</div>

Using a range for me still has a performance difference

```nohighlight

julia> @btime f($g,1:3)
  1.812 ns (0 allocations: 0 bytes)
1:3

julia> @btime getindex($g,1:3)
  26.105 ns (1 allocation: 112 bytes)
3-element Array{Int64,1}:
 1
 2
 3

```

Interesting, it actually returns a range there, so it optimized array reference out.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 2, 2019, 4:57pm UTC](https://discourse.julialang.org/t/strange-behavior-with-array-calls/19200/5 "2019-01-02T16:57:28Z")

</div>

Looks like this is an actual bug here

```nohighlight
julia> y = [4,5,6];

julia> @btime f($y,1:3)
  2.166 ns (0 allocations: 0 bytes)
1:3

julia> @btime getindex($y,1:3)
  25.259 ns (1 allocation: 112 bytes)
3-element Array{Int64,1}:
 1
 2
 3

```

That is the wrong result…

```nohighlight
julia> f(y,7)
7

```

should return an error because 3-element array, but returns the value of index

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 2, 2019, 5:06pm UTC](https://discourse.julialang.org/t/strange-behavior-with-array-calls/19200/6 "2019-01-02T17:06:51Z")

</div>

I get the same results, Windows 10, Julia 1.0.1:

```julia
julia> @btime f($g,2)
  1.026 ns (0 allocations: 0 bytes)
2

julia> @btime getindex($g,2)
  1.026 ns (0 allocations: 0 bytes)
2

julia> @btime f($g,1:3)
  27.968 ns (1 allocation: 112 bytes)
3-element Array{Int64,1}:
 1
 2
 3

julia> @btime getindex($g,1:3)
  27.712 ns (1 allocation: 112 bytes)
3-element Array{Int64,1}:
 1
 2
 3

julia> @btime f($g,7)
  1.026 ns (0 allocations: 0 bytes)
86067088

julia> @btime getindex($g,7)
  1.026 ns (0 allocations: 0 bytes)
86067088

```

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 2, 2019, 5:07pm UTC](https://discourse.julialang.org/t/strange-behavior-with-array-calls/19200/7 "2019-01-02T17:07:45Z")

</div>

Figured out, `julia-2:1.0.3-2` on arch linux is broken, when I revert to `julia-2:1.0.3-1` it’s normal.

---

<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 2, 2019, 5:16pm UTC](https://discourse.julialang.org/t/strange-behavior-with-array-calls/19200/8 "2019-01-02T17:16:46Z")

</div>

Are you sure you didn’t misdefine `f` in your last REPL session? What commit is `julia-2:1.0.3-2` on? Would be good to get to the bottom of this.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 2, 2019, 5:19pm UTC](https://discourse.julialang.org/t/strange-behavior-with-array-calls/19200/9 "2019-01-02T17:19:42Z")

</div>

Updated again and restarted REPL, guess it was just something quirky that happened in that session…

Now that I look back at the history, I did make this experimental definition for `f` also in the same session

```nohighlight
f(x::Vector{M},m=M) where M = m

```

That’s why `f` had 3 methods defined for it, and is the cause for the strange behavior.

Please feel free to delete this entire thread if you want to.
