# Spurious allocation?

**URL:** <https://discourse.julialang.org/t/spurious-allocation/3751>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 16, 2017, 4:08pm UTC](https://discourse.julialang.org/t/spurious-allocation/3751 "2017-05-16T16:08:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [May 16, 2017, 4:08pm UTC](https://discourse.julialang.org/t/spurious-allocation/3751/1 "2017-05-16T16:08:51Z")

</div>

This is a really simple problem, and I’m not sure if I’m missing anything obvious here, but why is the following operation allocating memory?

```julia
B = Matrix{Float64}(10,10^5);
@time for j=1:10^5,i=1:10
    B[i,j] = 1.0
end
0.023271 seconds (994.89 k allocations: 15.181 MB, 7.25% gc time)

```

---

<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:** [May 16, 2017, 4:18pm UTC](https://discourse.julialang.org/t/spurious-allocation/3751/2 "2017-05-16T16:18:34Z")

</div>

Don’t benchmark in global scope.

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [May 16, 2017, 4:18pm UTC](https://discourse.julialang.org/t/spurious-allocation/3751/3 "2017-05-16T16:18:37Z")

</div>

Nevermind, this was a global scope issue…

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 16, 2017, 4:48pm UTC](https://discourse.julialang.org/t/spurious-allocation/3751/4 "2017-05-16T16:48:08Z")

</div>

One should default to [BenchmarkTools.jl](https://github.com/JuliaCI/BenchmarkTools.jl) for timing. I have found that `@time`, `tic()`, `toc()` can, for whatever reason, be quite inaccurate, especially for small (\lesssim 10\mu s) intervals, and, more importantly, it includes compile time.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [May 16, 2017, 5:15pm UTC](https://discourse.julialang.org/t/spurious-allocation/3751/5 "2017-05-16T17:15:39Z")

</div>

> [@ExpandingMan](#):
>
> it includes compile time.

Is there confirmation anywhere that BenchmarkTools does _not_ include compile time? Does it just discard the first run’s time?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 16, 2017, 5:24pm UTC](https://discourse.julialang.org/t/spurious-allocation/3751/6 "2017-05-16T17:24:31Z")

</div>

I’m not sure exactly how it works (I would assume it somehow explicitly forces the code to be compiled before it is run at all), but yes, it does _not_ include compile time. A few casual experiments with `@benchmark` should confirm this to your satisfaction.

---

<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:** [May 16, 2017, 5:40pm UTC](https://discourse.julialang.org/t/spurious-allocation/3751/7 "2017-05-16T17:40:13Z")

</div>

Also note that the package does not magically solve all issues with benchmarks. Replacing the `@time` in the code above with `@benchmark` or `@btime` will **not** give the correct benchmark result. It’ll still include global access time. For these usage, see the doc [https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#introduction](https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#introduction)
