# Performance difference between two code

**URL:** <https://discourse.julialang.org/t/performance-difference-between-two-code/1557>\
**Category:** New to Julia\
**Created:** [January 18, 2017, 12:12am UTC](https://discourse.julialang.org/t/performance-difference-between-two-code/1557 "2017-01-18T00:12:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![emirahmet](https://avatars.discourse-cdn.com/v4/letter/e/f0a364/32.png) [@emirahmet](https://discourse.julialang.org/u/emirahmet)\
**Post date:** [January 18, 2017, 12:12am UTC](https://discourse.julialang.org/t/performance-difference-between-two-code/1557/1 "2017-01-18T00:12:40Z")

</div>

Hello,

I have two codes that are doing the exact same thing. I put them below. In the latter one, we use a multi-dimensional array instead of x. The former one is substantially faster than the other, and uses smaller number of allocations. What is the reason of it?

Thanks in advance.

—

```julia
x = [0.76,-1.13,1.33];
@time for i=1:450000
       dot(x,[1.,2.,3.])
      end

```

0.034094 seconds (900.00 k allocations: 96.130 MB, 11.11% gc time)

—

```julia
X = zeros(3,3,3,100);
X[:,1,1,1] = x;
@time for i=1:450000
       dot(X[:,1,1,1],[1.,2.,3.])
       end

```

0.226584 seconds (2.70 M allocations: 130.463 MB, 3.27% gc time)

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [January 18, 2017, 12:23am UTC](https://discourse.julialang.org/t/performance-difference-between-two-code/1557/2 "2017-01-18T00:23:40Z")

</div>

This might be because indexing allocates memory, but it’s hard to tell for sure since you seem to be benchmarking in the global scope which causes problems.

---

<div class="post-metadata">

**Author:** ![ararslan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ararslan/32/3825_2.png) [@ararslan](https://discourse.julialang.org/u/ararslan)\
**Post date:** [January 18, 2017, 6:06am UTC](https://discourse.julialang.org/t/performance-difference-between-two-code/1557/3 "2017-01-18T06:06:16Z")

</div>

To add to what John said, I recommend checking out the [BenchmarkTools.jl](https://github.com/JuliaCI/BenchmarkTools.jl) package for a more comprehensive (and easy to use!) approach to benchmarking.

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [January 18, 2017, 6:32am UTC](https://discourse.julialang.org/t/performance-difference-between-two-code/1557/4 "2017-01-18T06:32:32Z")

</div>

```julia
using BenchmarkTools

foo(x,y) = dot(x[:,1,1,1], y)
bar(x,y) = @inbounds dot(x[:,1,1,1], y)
baz(x,y) = dot(x, y)

x = [0.76,-1.13,1.33];
X = zeros(3,3,3,100);
X[:,1,1,1] = x;
y = [1.,2,3]

@benchmark foo($X,$y)
@benchmark bar($X,$y)
@benchmark baz($x,$y)

```

```julia
julia> @benchmark foo($X,$y)
BenchmarkTools.Trial: 
  memory estimate: 128.00 bytes
  allocs estimate: 2
  --------------
  minimum time: 52.000 ns (0.00% GC)
  median time: 58.526 ns (0.00% GC)
  mean time: 64.345 ns (8.17% GC)
  maximum time: 1.080 μs (93.56% GC)
  --------------
  samples: 10000
  evals/sample: 984
  time tolerance: 5.00%
  memory tolerance: 1.00%

julia> @benchmark bar($X,$y)
BenchmarkTools.Trial: 
  memory estimate: 128.00 bytes
  allocs estimate: 2
  --------------
  minimum time: 51.291 ns (0.00% GC)
  median time: 55.050 ns (0.00% GC)
  mean time: 61.033 ns (9.23% GC)
  maximum time: 1.188 μs (92.17% GC)
  --------------
  samples: 10000
  evals/sample: 984
  time tolerance: 5.00%
  memory tolerance: 1.00%

julia> @benchmark baz($x,$y)
BenchmarkTools.Trial: 
  memory estimate: 0.00 bytes
  allocs estimate: 0
  --------------
  minimum time: 14.059 ns (0.00% GC)
  median time: 15.109 ns (0.00% GC)
  mean time: 15.060 ns (0.00% GC)
  maximum time: 29.657 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 998
  time tolerance: 5.00%
  memory tolerance: 1.00%

```
