# Benchmarking function in local scope

**URL:** https://discourse.julialang.org/t/benchmarking-function-in-local-scope/110228
**Category:** General Usage
**Tags:** package, benchmark, benchmarktools
**Created:** [February 14, 2024, 11:35pm UTC](https://discourse.julialang.org/t/benchmarking-function-in-local-scope/110228 "2024-02-14T23:35:46Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![leespen1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leespen1/32/221928_2.png) [@leespen1](https://discourse.julialang.org/u/leespen1)
#### Post date: [February 14, 2024, 11:35pm UTC](https://discourse.julialang.org/t/benchmarking-function-in-local-scope/110228/1 "2024-02-14T23:35:46Z")

</div>

I have a function that I would like to benchmark. To that end, I would like to time the function for several different argument values, and plot how the runtime changes with the argument values. I can do this using the `@elapsed` macro, but to get a more accurate/reliable result I would like to use the `@belapsed` macro from `BenchmarkTools`. E.g.

```julia
julia> using BenchmarkTools

julia> function f()
         a = "Hello World"
         t = @belapsed println(a)
       end
f (generic function with 1 method)

julia> f()
ERROR: UndefVarError: `a` not defined

```

I cannot use `@belapsed` because the function I am benchmarking uses variables in the local scope of the function it is called within. According to the `BenchmarkTools` documentation: " `@benchmark` is evaluated in global scope, even if called from local scope." I guess the same is true of `@belapsed`.

This is a surprising design choice to me, but I don’t see a workaround besides executing the body of the outer function in the global scope, which would be very inconvenient.

Does anyone know a workaround or another package to use so I can get a reliable estimate of the runtime for a function, even when called inside the scope of another function?

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [February 14, 2024, 11:43pm UTC](https://discourse.julialang.org/t/benchmarking-function-in-local-scope/110228/2 "2024-02-14T23:43:35Z")

</div>

Try escaping using `println($a)`.

---

<div class="post-metadata">

### Author: ![leespen1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leespen1/32/221928_2.png) [@leespen1](https://discourse.julialang.org/u/leespen1)
#### Post date: [February 16, 2024, 7:11pm UTC](https://discourse.julialang.org/t/benchmarking-function-in-local-scope/110228/3 "2024-02-16T19:11:39Z")

</div>

That works, thanks a bunch! I did not know that the `$` operator could be used like that. May I ask, what exactly does `$` do outside of a string? From the REPL, if I just do `$a` for some variable `a`, I get an error:

```julia
julia> $a
ERROR: syntax: "$" expression outside quote around REPL[103]:1

```

And the help string for `$` doesn’t help me much:

```julia
help?> $
search: $

  $

  Interpolation operator for interpolating into e.g. strings and expressions.

  Examples
  ≡≡≡≡≡≡≡≡

  julia> name = "Joe"
  "Joe"
  
  julia> "My name is $name."
  "My name is Joe."

```

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [February 16, 2024, 7:29pm UTC](https://discourse.julialang.org/t/benchmarking-function-in-local-scope/110228/4 "2024-02-16T19:29:55Z")

</div>

`$` is a feature of the BenchmarkTools.jl macros. See the docs for more details.

> If the expression you want to benchmark depends on external variables, you should use [`$` to “interpolate”](https://juliaci.github.io/BenchmarkTools.jl/stable/manual/#Interpolating-values-into-benchmark-expressions) them into the benchmark expression to [avoid the problems of benchmarking with globals](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables). Essentially, any interpolated variable `$x` or expression `$(...)` is “pre-computed” before benchmarking begins

[https://juliaci.github.io/BenchmarkTools.jl/stable/](https://juliaci.github.io/BenchmarkTools.jl/stable/)

---

<div class="post-metadata">

### Author: ![leespen1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leespen1/32/221928_2.png) [@leespen1](https://discourse.julialang.org/u/leespen1)
#### Post date: [February 16, 2024, 7:48pm UTC](https://discourse.julialang.org/t/benchmarking-function-in-local-scope/110228/5 "2024-02-16T19:48:56Z")

</div>

Awesome, thanks!
