They are not the same (you can explore this with @edit, or the debugger). The first creates a temporary array for A .> 0, and then collects the true indexes. Both of these operations are very fast.
The second one goes through the generic iterator path. Perhaps it could optimized more, I am sure PRs doing this would be welcome.
Thanks for your answer. Perhaps some warning in the documentation (Arrays · The Julia Language), about the case where the second argument is an array, would be useful.
I don’t think there is an inherent reason for the performance difference here (which would of course warrant documentation), it is just waiting for someone to optimize it.
Sorry I am new,
but both of these gives the same result say if i want to find the indexes where the maximum values occur, the first one is way faster…
Can you please explain this more
For every iteration, this calculates the maximum again and again. If there are 100x100 element, it will pass over the array 10,000 times. You only need to calculate the maximum once.
julia> x="world" # string
"hello $x" # string interpolation
"hello world"
julia> y=:( a += 1 ) # expression
:( if true; $y end ) # expression interpolation
:(if true
#= REPL[2]:2 =#
a += 1
end)
Because @btime is a macro, it operates on an expression. The authors of BenchmarkTools decided that $ would capture the variable from the environment and interpolate it into the expression, while passing through type information and blocking constant propagation.
All that to say: when you’re building strings and expressions, and sometimes when calling macros, $ can be useful. But for normal code, $ shouldn’t be dangling around.