El domingo, 15 de noviembre de 2015, 17:35:45 (UTC-6), Steven G. Johnson escribió: > > 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. >
However, if you are doing actual calculations inside the loop (rather than just storing the values), then in my experience the performance difference is much smaller.