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)


Reply via email to