Map a scalar function over collection of arrays

Your benchmarks were misleading because they used a global variable x. When using BenchmarkTools, you have to be careful to splice in the values of globals:

julia> @btime broadcast.(rad2deg,x);
  7.395 μs (11 allocations: 480 bytes)

julia> @btime broadcast.(rad2deg,$x);
  76.135 ns (5 allocations: 256 bytes)

julia> @btime map(x->rad2deg.(x), $x);
  68.809 ns (3 allocations: 224 bytes)

julia> @btime [rad2deg.(i) for i in $x];
  102.691 ns (4 allocations: 304 bytes)

julia> @btime map.(rad2deg, $x);
  86.099 ns (5 allocations: 256 bytes)

So, the broadcast.(....) solution is just fine.

3 Likes