# Help using @btime correctly

**URL:** https://discourse.julialang.org/t/help-using-btime-correctly/43433
**Category:** Performance
**Created:** [July 21, 2020, 2:14pm UTC](https://discourse.julialang.org/t/help-using-btime-correctly/43433 "2020-07-21T14:14:44Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jackf](https://avatars.discourse-cdn.com/v4/letter/j/b4bc9f/32.png) [@jackf](https://discourse.julialang.org/u/jackf)
#### Post date: [July 21, 2020, 2:14pm UTC](https://discourse.julialang.org/t/help-using-btime-correctly/43433/1 "2020-07-21T14:14:44Z")

</div>

[https://github.com/JuliaLang/julia/issues/36751#issuecomment-661883249](https://github.com/JuliaLang/julia/issues/36751#issuecomment-661883249)

I’ve been told I’m not using @btime correctly. Can someone please explain how to profile the allocations of this function without allocating the output? If I avoid returning the output or wrap it in another function, the whole computation gets compiled away.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [July 21, 2020, 2:23pm UTC](https://discourse.julialang.org/t/help-using-btime-correctly/43433/2 "2020-07-21T14:23:24Z")

</div>

Like this:

```julia
julia> @btime foo($vals)
  754.432 ns (0 allocations: 0 bytes)

```

For an explanation of the use of `$` here, check out the [BenchmarkTools manual](https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#interpolating-values-into-benchmark-expressions).

---

<div class="post-metadata">

### Author: ![jackf](https://avatars.discourse-cdn.com/v4/letter/j/b4bc9f/32.png) [@jackf](https://discourse.julialang.org/u/jackf)
#### Post date: [July 21, 2020, 2:31pm UTC](https://discourse.julialang.org/t/help-using-btime-correctly/43433/3 "2020-07-21T14:31:46Z")

</div>

Thanks. So the cause of the allocations has nothing to do with the output (which is just an Int64 - should be on the stack anyway), but is to do with vals being a non-constant global (which requires some type checking or dispatch or something, that is being measured)?

---

<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: [July 21, 2020, 2:34pm UTC](https://discourse.julialang.org/t/help-using-btime-correctly/43433/4 "2020-07-21T14:34:27Z")

</div>

> [@jackf](#):
>
> So the cause of the allocations has nothing to do with the output (which is just an Int64 - should be on the stack anyway),

No. A `Int64` is not always on the stack. In fact, it is almost never on the stack. Mostly either not existing, in the register, or on the heap. The allocation of the result stilll **IS** the issue and it happens every time the result can’t be inferred including when it needs to be stored in a global.

---

<div class="post-metadata">

### Author: ![jackf](https://avatars.discourse-cdn.com/v4/letter/j/b4bc9f/32.png) [@jackf](https://discourse.julialang.org/u/jackf)
#### Post date: [July 21, 2020, 3:21pm UTC](https://discourse.julialang.org/t/help-using-btime-correctly/43433/5 "2020-07-21T15:21:27Z")

</div>

Ah, that makes sense, thank you.

I’m still curious to understand why the allocating version does or does not allocate depending on the contents of the vector. It seems like for a vector of ones, if I set more than the first 511 values to NaN, it allocates.

---

<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: [July 21, 2020, 3:44pm UTC](https://discourse.julialang.org/t/help-using-btime-correctly/43433/6 "2020-07-21T15:44:40Z")

</div>

The boxes (heap allocated) values for small integers are cached.
