# Allocations in function timing

**URL:** <https://discourse.julialang.org/t/allocations-in-function-timing/16081>\
**Category:** Performance\
**Created:** [October 9, 2018, 4:30pm UTC](https://discourse.julialang.org/t/allocations-in-function-timing/16081 "2018-10-09T16:30:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [October 9, 2018, 4:30pm UTC](https://discourse.julialang.org/t/allocations-in-function-timing/16081/1 "2018-10-09T16:30:05Z")

</div>

Sorry for hijacking the topic, but as I was playing with the algorithms, I noticed that given this function:

```julia
julia> function main(n)
         r = 0
         for i = 1:n
           i -= 1
           r += 1
         end
         return r
       end
main (generic function with 1 method)

```

if we run

```julia
julia> @time main(10)
  0.000005 seconds (4 allocations: 160 bytes)
10

julia> @time main(100)
  0.000003 seconds (4 allocations: 160 bytes)
100

julia> @time main(1000)
  0.000002 seconds (5 allocations: 176 bytes)
1000

```

why does `main(1000)` end up with 5 allocations?

I looked at the code with all the `@code_*` macros and there was no difference between calling it with 100 and 1000.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 9, 2018, 4:31pm UTC](https://discourse.julialang.org/t/allocations-in-function-timing/16081/2 "2018-10-09T16:31:07Z")

</div>

Use BenchmarkTooks.jl for this kind of benchmarking.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [October 9, 2018, 4:43pm UTC](https://discourse.julialang.org/t/allocations-in-function-timing/16081/3 "2018-10-09T16:43:49Z")

</div>

Thanks @dpsanders – I keep forgetting about BenchmarkTools !

Now consistently getting 0:

```julia
julia> @btime main(1000)
  2.324 ns (0 allocations: 0 bytes)

```
