# 3 different ways to create an array have different performance

**URL:** <https://discourse.julialang.org/t/3-different-ways-to-create-an-array-have-different-performance/9636>\
**Category:** Performance\
**Created:** [March 10, 2018, 8:10pm UTC](https://discourse.julialang.org/t/3-different-ways-to-create-an-array-have-different-performance/9636 "2018-03-10T20:10:59Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [March 10, 2018, 8:36pm UTC](https://discourse.julialang.org/t/3-different-ways-to-create-an-array-have-different-performance/9636/2 "2018-03-10T20:36:36Z")

</div>

Don’t benchmark in global scope:

```julia
julia> const PP_DD = Normal(4.5, 1)
Distributions.Normal{Float64}(μ=4.5, σ=1.0)

julia> f1() = [Int(round(rand(PP_DD))) for i=1:100]
f1 (generic function with 1 method)

julia> f2() = Int.(round.(rand(PP_DD, 100)));

julia> f3() = begin
         dpc = zeros(Int64, 100)
         for i = 1:100
           dpc[i]=Int(round(rand(PP_DD)))
         end
       end
f3 (generic function with 1 method)

julia> @btime f1();
  1.836 μs (1 allocation: 896 bytes)

julia> @btime f2();
  1.647 μs (2 allocations: 1.75 KiB)

julia> @btime f3();
  1.890 μs (1 allocation: 896 bytes)

```

---

_[View the full topic](https://discourse.julialang.org/t/3-different-ways-to-create-an-array-have-different-performance/9636)._
