# Generators speed

**URL:** <https://discourse.julialang.org/t/generators-speed/3715>\
**Category:** New to Julia\
**Created:** [May 15, 2017, 12:21pm UTC](https://discourse.julialang.org/t/generators-speed/3715 "2017-05-15T12:21:08Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 15, 2017, 12:30pm UTC](https://discourse.julialang.org/t/generators-speed/3715/2 "2017-05-15T12:30:34Z")

</div>

You should not benchmark in global scope.

```julia
using BenchmarkTools

N = 10000000
A = randn(N)

sum1(A) = sum(A)
sum2(A) = sum(A[i] for i in 1:N)
sum3(A) = sum(a for a in A)

```

then

```julia
julia> @benchmark sum1($A)
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 4.576 ms (0.00% GC)
  median time: 5.744 ms (0.00% GC)
  mean time: 5.672 ms (0.00% GC)
  maximum time: 6.901 ms (0.00% GC)
  --------------
  samples: 881
  evals/sample: 1

julia> @benchmark sum2($A)
BenchmarkTools.Trial: 
  memory estimate: 112 bytes
  allocs estimate: 5
  --------------
  minimum time: 12.764 ms (0.00% GC)
  median time: 12.885 ms (0.00% GC)
  mean time: 12.972 ms (0.00% GC)
  maximum time: 16.242 ms (0.00% GC)
  --------------
  samples: 386
  evals/sample: 1

julia> @benchmark sum3($A)
BenchmarkTools.Trial: 
  memory estimate: 48 bytes
  allocs estimate: 3
  --------------
  minimum time: 12.761 ms (0.00% GC)
  median time: 12.874 ms (0.00% GC)
  mean time: 12.941 ms (0.00% GC)
  maximum time: 16.360 ms (0.00% GC)
  --------------
  samples: 387
  evals/sample: 1

```

so the difference is 2-3x. Above using `v0.6-rc1`.

---

_[View the full topic](https://discourse.julialang.org/t/generators-speed/3715)._
