# @btime introduces a new scope?

**URL:** <https://discourse.julialang.org/t/btime-introduces-a-new-scope/52818>\
**Category:** General Usage\
**Tags:** question, benchmarktools\
**Created:** [January 4, 2021, 11:33am UTC](https://discourse.julialang.org/t/btime-introduces-a-new-scope/52818 "2021-01-04T11:33:50Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [January 4, 2021, 11:38am UTC](https://discourse.julialang.org/t/btime-introduces-a-new-scope/52818/2 "2021-01-04T11:38:30Z")

</div>

OK, I found an answer to this elsewhere on discourse, so I’ll answer my own question and hope that it helps someone else:

> [@@btime eats my variables](https://discourse.julialang.org/t/btime-eats-my-variables/23751/6):
>
> 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\> 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 thi…

Variables in the macro need to be global, not local, scope variables. $ can be used to interpolate to the value of the local variable in the @btime call.

Here’s the simple correction to my example:

```julia
for k ∈ [5,10,20]
    println(k)
    @btime v1(x[1],$k);
    @btime v2(x[1],$k);
end

```

and the output:

```julia
5
  107.532 ns (2 allocations: 144 bytes)
  63.393 ns (2 allocations: 144 bytes)
10
  187.909 ns (2 allocations: 176 bytes)
  80.880 ns (2 allocations: 176 bytes)
20
  358.810 ns (2 allocations: 256 bytes)
  115.605 ns (2 allocations: 256 bytes)

```

My “fast” version is sublinear in problem size.

---

_[View the full topic](https://discourse.julialang.org/t/btime-introduces-a-new-scope/52818)._
