# Interpolation in macro calls

**URL:** <https://discourse.julialang.org/t/interpolation-in-macro-calls/25530>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 21, 2019, 3:59pm UTC](https://discourse.julialang.org/t/interpolation-in-macro-calls/25530 "2019-06-21T15:59:58Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [June 21, 2019, 10:47pm UTC](https://discourse.julialang.org/t/interpolation-in-macro-calls/25530/3 "2019-06-21T22:47:32Z")

</div>

> [@mauro3](#):
>
> So `@btime` then presumably does some tricks to make the `x` not a slow global. Maybe such as doing a `const xyz = x` where `xyz` would be a gensym. But I’m just guessing.

Yes, that is more or less what happens. Below is an expansion of `@btime cos($(x+1))`, with comments explaining the parameters of `generate_benchmark`:

```julia
julia> using BenchmarkTools
julia> using MacroTools
julia> (@macroexpand @btime cos($(x+1))) |> rmlines |> MacroTools.alias_gensyms
quote
    local manatee = begin
      (BenchmarkTools).generate_benchmark_definition(
         Main, # eval_module
         Symbol[], # out_vars
         Symbol[Symbol("cheetah")], # setup_vars
         $(Expr(:copyast, :($(QuoteNode(:(cos(cheetah))))))), (Core._expr)(:block, $(Expr(:copyast, :($(QuoteNode(nothing))))), # code
         (Core._expr)(:(=), Symbol("cheetah"), x + 1)), # setup
         $(Expr(:copyast, :($(QuoteNode(nothing))))), # teardown
         (BenchmarkTools.Parameters)() # params
      )                                                                                                                                                                        
    end
    (BenchmarkTools).warmup(manatee)
    (BenchmarkTools).tune!(manatee)
    local (sanddollar, bison) = (BenchmarkTools).run_result(manatee)
    local crow = (BenchmarkTools).minimum(sanddollar)
    local donkey = (BenchmarkTools).allocs(crow)
    println(" ", (BenchmarkTools).prettytime((BenchmarkTools).time(crow)), " (", donkey, " allocation", if donkey == 1
            ""
        else
            "s"
        end, ": ", (BenchmarkTools).prettymemory((BenchmarkTools).memory(crow)), ")")
    bison
end

```

In particular, you see that the “code” has been transformed into basically `cos(cheetah)`, with a “setup” which says: `cheetah = x+1`.

  

You’ll probably be able to find the part of the `BenchmarkTools` which does this if you search for it. In the [`GFlops.jl`](https://github.com/triscale-innov/GFlops.jl) package, I try to implement the same kind of feature (separation between code and preliminary setup) [here](https://github.com/triscale-innov/GFlops.jl/blob/master/src/count_ops.jl#L12), in order to use it [there](https://github.com/triscale-innov/GFlops.jl/blob/master/src/count_ops.jl#L20).

Hopefully you’ll find this example useful.

---

_[View the full topic](https://discourse.julialang.org/t/interpolation-in-macro-calls/25530)._
