# Zeros slower than Array comprehension

**URL:** <https://discourse.julialang.org/t/zeros-slower-than-array-comprehension/15980>\
**Category:** Performance\
**Tags:** question\
**Created:** [October 6, 2018, 10:25pm UTC](https://discourse.julialang.org/t/zeros-slower-than-array-comprehension/15980 "2018-10-06T22:25:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![antcap96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antcap96/32/7654_2.png) [@antcap96](https://discourse.julialang.org/u/antcap96)\
**Post date:** [October 6, 2018, 10:25pm UTC](https://discourse.julialang.org/t/zeros-slower-than-array-comprehension/15980/1 "2018-10-06T22:25:32Z")

</div>

When testing some things in the REPL I tried creating a 100x100 matrix of zeros and found it strange that the built-in zeros was slower than creating such a matrix using a Array comprehension.

```julia
julia> @btime zeros(Float64,100,100);
  7.369 μs (2 allocations: 78.20 KiB)

julia> @btime Float64[0.0 for i=1:100, j=1:100];
  4.342 μs (2 allocations: 78.20 KiB)

```

Why is this the case?

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [October 6, 2018, 11:08pm UTC](https://discourse.julialang.org/t/zeros-slower-than-array-comprehension/15980/2 "2018-10-06T23:08:46Z")

</div>

They’re about the same if you do

```julia
@btime zeros($Float64,100,100);

```

instead. See [https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#interpolating-values-into-benchmark-expressions](https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#interpolating-values-into-benchmark-expressions).

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 6, 2018, 11:38pm UTC](https://discourse.julialang.org/t/zeros-slower-than-array-comprehension/15980/3 "2018-10-06T23:38:27Z")

</div>

> [@tkoolen](#):
>
> ```julia
> @btime zeros($Float64,100,100);
> 
> ```

This is wrong. There’s nothing wrong about `@btime zeros(Float64, 100, 100)`. What you suggest will only make it hit [Overhead caused by widening · Issue #71 · JuliaCI/BenchmarkTools.jl · GitHub](https://github.com/JuliaCI/BenchmarkTools.jl/issues/71)

That said, I get more timing variations for the `zeros` than for the comprehension. Neither is consistently faster than the other. Could be memory address related.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 7, 2018, 1:37am UTC](https://discourse.julialang.org/t/zeros-slower-than-array-comprehension/15980/4 "2018-10-07T01:37:02Z")

</div>

> [@yuyichao](#):
>
> This is wrong. There’s nothing wrong about `@btime zeros(Float64, 100, 100)` .

In particular, you only need to use `$` interpolation on non-constant variables (or expressions that you want to evaluate before benchmarking). `Float64` is a constant.
