# @btime eats my variables

**URL:** <https://discourse.julialang.org/t/btime-eats-my-variables/23751>\
**Category:** New to Julia\
**Tags:** benchmarktools\
**Created:** [May 1, 2019, 8:39pm UTC](https://discourse.julialang.org/t/btime-eats-my-variables/23751 "2019-05-01T20:39:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![freemint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freemint/32/20013_2.png) [@freemint](https://discourse.julialang.org/u/freemint)\
**Post date:** [May 1, 2019, 8:39pm UTC](https://discourse.julialang.org/t/btime-eats-my-variables/23751/1 "2019-05-01T20:39:12Z")

</div>

Hello,  
this is my first post, please be kind.  
I want to test hypothesis where the allocation happens and time is spent in my functions. I imagine doing it the following way:

```julia
function fr(x::Int64,y::Int64) 
       a = 0
       @time for i=1:1000000000 a = x^x end
       return y^y-a 
end
fr(8,9)

```

Which yields

```julia
4.075226 seconds

```

Having to introduce a loop so you see what is going on is one reason why @btime is usually preferred. However

```julia
using BenchmarkTools
function fr(x::Int64,y::Int64) 
       @btime a = x^x 
       return y^y-a 
end
fr(8,9)

```

Causes Julia much pain:

```julia
ERROR: UndefVarError: x not defined

```

My versioninfo() is:

```julia
Julia Version 1.0.3
Commit 099e826241 (2018-12-18 01:34 UTC)
Platform Info:
  OS: Linux (x86_64-redhat-linux)
  CPU: Intel(R) Core(TM) i5-8XXU CPU @ X.XGHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, skylake)

```

Any ideas how i can get a better idea about the performance behavior of parts of my program?

As far as i understand it:  
@btime is expanded as a macro and executed at parse/compile time, at which x is not known

I do not understand why my first case works at all since it’s a macro too.

Sincerely, freemint

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [May 1, 2019, 8:51pm UTC](https://discourse.julialang.org/t/btime-eats-my-variables/23751/2 "2019-05-01T20:51:05Z")

</div>

Hi freemint!

Sounds like profiling would be better suited to your task. There is a section on it in the manual.

PS: looks like you messed up with the ` ` `-quoting, probably at the top.

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [May 1, 2019, 8:55pm UTC](https://discourse.julialang.org/t/btime-eats-my-variables/23751/3 "2019-05-01T20:55:13Z")

</div>

For profiling, [this package may be helpful](https://github.com/tkluck/StatProfilerHTML.jl). (Disclosure: I wrote it.)

---

<div class="post-metadata">

**Author:** ![freemint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freemint/32/20013_2.png) [@freemint](https://discourse.julialang.org/u/freemint)\
**Post date:** [May 1, 2019, 9:30pm UTC](https://discourse.julialang.org/t/btime-eats-my-variables/23751/4 "2019-05-01T21:30:34Z")

</div>

Thank you for your pointers.  
I was already aware of the extensive Profiling features in Julia.  
The problem is they do to much and break my workflow.  
I like to make a hypothesis, where something is going wrong and then check it.  
@tkluck while your package indeed presents a nice gui, is there a way to make it check allocations too?

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [May 1, 2019, 9:46pm UTC](https://discourse.julialang.org/t/btime-eats-my-variables/23751/5 "2019-05-01T21:46:53Z")

</div>

> [@freemint](#):
>
> The problem is they do to much and break my workflow.  
> I like to make a hypothesis, where something is going wrong and then check it.

I absolutely love that attitude – as I’m sure you’ve seen, many junior developers attack hard-to-solve bugs by just trying to change things randomly, adding verbose logging, or stepping through. Bisecting through structured, repeated hypothesis validation/refuting is one of the most productive things to learn.

However, for finding performance issues, there’s absolutely no way to do it without data. Statistical profiling and flamegraphs _need_ to be part of your workflow. It’s absolutely worth getting used to!

> is there a way to make it check allocations too?

Not at the moment: julia’s `--track-allocation` option is a bit less ergonomic than its statistical profiling. I usually just eyeball its output through `cat *.<pid>.mem`… I’d love to add it as a feature to the package, though!

---

<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:** [May 1, 2019, 10:08pm UTC](https://discourse.julialang.org/t/btime-eats-my-variables/23751/6 "2019-05-01T22:08:52Z")

</div>

Welcome!

The reason for your issue is that `@btime` is designed to operate at global scope, so any variables it references need to be globals. That doesn’t work in your case because `x` is a local, not a global variable. Fortunately, this is easy to work around: you can use `$` to interpolate the _value_ of `x` into the expression being benchmarked:

```julia
julia> function f(x)
         @btime $x^2
       end
f (generic function with 1 method)

julia> f(1)
  0.024 ns (0 allocations: 0 bytes)

```

Note also that this isn’t a general property of macros, but just a specific result of the fact that `@btime` and `@benchmark` are designed to treat all variables inside the expression you’re benchmarking as globals.
