When to use broadcasting with . vs map

map (or a comprehension) is probably faster if seq is a generic iterable collection rather than an array, since broadcasting first calls collect to convert iterables into arrays. For example:

julia> @btime sqrt.(i^2 for i in 1:100);
  325.522 ns (2 allocations: 1.75 KiB)

julia> @btime map(sqrt, i^2 for i in 1:100);
  235.057 ns (1 allocation: 896 bytes)

Notice that the memory allocation is doubled in the broadcast case, because of the extra array allocated by collect.

30 Likes