If you use the FastAnonymous package then they should be much closer: julia> using FastAnonymous
julia> fsin = @anon x->sin(x) ##7954 (constructor with 2 methods) julia> fsin(0.3) 0.29552020666133955 julia> @time map(fsin, data); elapsed time: 0.285690205 seconds (80324156 bytes allocated) julia> @time map(fsin, data); elapsed time: 0.23800597 seconds (80000128 bytes allocated) --Tim On Thursday, September 18, 2014 12:31:14 PM Johan Sigfrids wrote: > So I was looking at allocations in some code and I noticed I sped things up > significantly by changing map to a list comprehension. Doing some > microbenchmarking I noticed that map allocates far more memory than a list > comprehension. Shouldn't they essentially be doing the same thing? > > data = rand(10000000) > > > > function f1(data) > > [sin(i) for i in data] > > end > > > > function f2(data) > > map(sin, data) > > end > > > > function f3(data) > > out = zeros(data) > > for i in 1:length(data) > > out[i] = sin(data[i]) > > end > > out > > end > > > > f1([0.1, 0.2]) > > f2([0.1, 0.2]) > > f3([0.1, 0.2]) > > sin([0.1, 0.2]) > > @time f1(data) > > @time f2(data) > > @time f3(data) > > @time sin(data); > > > elapsed time: 0.375611486 seconds (80000128 bytes allocated) > elapsed time: 1.707264865 seconds (400000128 bytes allocated, 28.03% gc > time) elapsed time: 0.359320195 seconds (80000128 bytes allocated) > elapsed time: 0.307740829 seconds (80000128 bytes allocated)
