On version 0.5, after warm-up. (Notice that I use collect() instead of [], 
since the latter doesn't work like that anymore)

function test_broad(N)
    @time for _ in 1:N broadcast( (x...) -> +(x...), collect(1:1000), 
collect(1001:2000) ) end
    @time for _ in 1:N broadcast( (x...) -> +(x...), 1:1000, 1001:2000 ) end
    @time for _ in 1:N broadcast(+, collect(1:1000), collect(1001:2000) ) 
end
    @time for _ in 1:N broadcast(+, 1:1000, 1001:2000 ) end
end
 
julia> test_broad(100000) 
  1.094695 seconds (1.50 M allocations: 2.302 GB, 19.12% gc time) 
  0.823986 seconds (2.00 M allocations: 831.604 MB, 13.52% gc time) 
  0.874488 seconds (1.30 M allocations: 2.298 GB, 23.24% gc time) 
  0.549401 seconds (2.00 M allocations: 833.130 MB, 16.72% gc time)


As you see, the anonymous function isn't a huge deal anymore. But another 
lesson is: Don't collect the ranges. Use them as they are, since they are 
much more memory efficient.

On Friday, May 13, 2016 at 11:54:52 PM UTC+2, Brandon Taylor wrote:
>
> I was wondering why the following code is so slow:
>
> @time broadcast( (x...) -> +(x...), [1:1000], [1001:2000] )
>
> In comparison to
>
> @time broadcast(+, [1:1000], [1001:2000] )
>
> and what would be a faster way to define an anonymous function with a 
> variable number of arguments? Note, I can't use zip because I can't 
> guarantee that the things being broadcast over are going to be the same 
> size.
>

Reply via email to