Hi,
Take a look at the following code:
using BenchmarkTools
s = ["$n" for n in 1:10000];
@btime map(x->parse(Int, x), s);
# 174.666 μs (3 allocations: 78.19 KiB)
@btime [x->parse(Int, x) for x in s];
# 3.401 μs (2 allocations: 64 bytes)
why is there such a huge difference?
I assumed these two implementations were practically interchangeable.’
Thanks,
DD
The second statement produces a vector of functions, not the values of the function x->parse(Int, x)
applied to the elements of s
!
julia> [x->parse(Int, x) for x in s]
10000-element Vector{var"#57#59"}:
#57 (generic function with 1 method)
#57 (generic function with 1 method)
#57 (generic function with 1 method)
[...]
Compare
julia> @btime [parse(Int, x) for x in $s]
329.400 μs (3 allocations: 78.19 KiB)
julia> @btime map(x->parse(Int, x), $s);
321.900 μs (2 allocations: 78.17 KiB)
Oops, you are obviously right.
Should have been:
@btime [parse(Int, x) for x in s];
# 174.500 μs (3 allocations: 78.19 KiB)
which is exactly the same