function prealloc(n)
    a = Array(Int, n)
    for i = 1:n
        a[i] = i
    end
    return a
end
function dynamic(n)
    a = Int[]
    for i = 1:n
        push!(a, i)
    end
    return a
end
@time prealloc(10^7);
@time dynamic(10^7);


On my machine, the preallocated version is 2.5–3x faster.  A significant 
but not overwhelming margin.

Reply via email to