Hi folks,
just a (probably) stupid question here. In another thread I’ve seen that people is testing functions operating on arrays, such that when the function is called, the array name is preceded by the $-symbol
function koly(x)
...
end
a=rand(10000);
@time koly($a)
and the result of the calculation seems to be fast, does no allocation and so there is garbage collection… looks perfect
Now I don’t know how these things are related because I can’t find the meaning of this $-syntax anywhere. Can somebody tell me/point me to the right documentation about this? Is it a feature of Julia 0.7? I ask becaus eI’m running 0.6.4, have tried it and the compiler complains
That is called “interpolation”, what happens is an expression is basically spliced or inserted into the code at that point, so that instead of referencing that value, it is directly inserted.
It works when an Expr or String is constructed, but not otherwise. Macros operate on expressions, so it works there, but it doesnt work in the REPL if you are not working with expression object or macros or strings.
If the expression you want to benchmark depends on external variables, you should use $ to “interpolate” them into the benchmark expression to avoid the problems of benchmarking with globals. Essentially, any interpolated variable x or expression (…) is “pre-computed” before benchmarking begins:
julia> A = rand(3,3);
julia> @btime inv($A); # we interpolate the global variable A with $A
1.191 μs (10 allocations: 2.31 KiB)
julia> @btime inv($(rand(3,3))); # interpolation: the rand(3,3) call occurs before benchmarking
1.192 μs (10 allocations: 2.31 KiB)
julia> @btime inv(rand(3,3)); # the rand(3,3) call is included in the benchmark time
1.295 μs (11 allocations: 2.47 KiB)
Another way of saying this: the $ interpolates the value of a into the expression :(kern($a)) so that the benchmarking code can avoid the performance penalty associated with looking up the global variable a.