BenchmarkTools.jl
BenchmarkTools.jl copied to clipboard
$x should become $(Ref(x))[]?
It's a bit annoying when you have to use this convoluted Ref trick to prevent the compiler from constant-folding the whole calculation (ala #130):
julia> @btime exp(1);
0.052 ns (0 allocations: 0 bytes)
julia> @btime exp($(Ref(1))[]);
13.785 ns (0 allocations: 0 bytes)
It would be nicer if we could just use @btime exp($1) and the $ did the trick automatically.
(Another case just came up on discourse. cc @stillyslalom)
+1 for a more convenient syntax, but I'm not sure whether $x is the right syntax. In my understanding of interpolation, x = 42; :(foo($x)) and foo(42) should be one and the same thing.
Individual macros can do whatever they want with interpolation syntax; it doesn't have to be limited to expression interpolation.
Of course you can write a macro such that @extremely_large_value_of_2(2+2) == 5, but that probably wouldn't be very useful unless you are running a T-shirt company.
A concrete example where (I believe) the proposed syntax would be problematic is x = 2.0; n = 2; @btime $(Ref(x))[]^$n. In this case, you might legitimately want n to be "hardwired" into the benchmark expression but not x.