BenchmarkTools Puzzles

I am following Chris’s tutorial of mit18337 course, in his lecture note(Optimizing Serial Code), there is an example about evaluating cost of heap allocation.

using LinearAlgebra, BenchmarkTools
function alloc_timer(n)
    A = rand(n,n)
    B = rand(n,n)
    C = rand(n,n)
    t1 = @belapsed $A*$B
    t2 = @belapsed mul!($C,$A,$B)
ns = 2 .^ (2:7)
res = [alloc_timer(n) for n in ns]
alloc   = [x[1] for x in res]
noalloc = [x[2] for x in res]

But when I changed the above piece of code like:

t1 = @belapsed A*B

The system gives me an error: A not defined

I have seen the manual(BenchmarkTools) of BenchmarkTools.jl, it says(hopefully my understanding is right) about the difference of with or without $:

  • Having $ means we are evaluating using passing values to a function.
  • Not using $ means we are using hard-coded form to evaluate

Back to my problem, I have tried using with and without $ outside a function. It works well.
I don’t know whether is it due to the internal realization or mechanism of the package BenchmarkTools.jl, or rather myself ignoring some syntax problem?