AFAIK the implementation of `push!` in Julia only occasionally allocates a bigger buffer for the new vector. This results in a very low average time for adding multiple elements (O(1) amortized time in complexity jargon).
Notice, `a = [a new_element ]` would in Julia do something else than `push!` - it would create a new array (with allocation), put the old `a` and `new_element` in it, and then rebind the symbol `a` to the new array, and let the garbage collector free the old array. This would be costly, so it's better to use push!. Even better, is to do the experiment and possibly report the answers on the list with working code, timings and versions. Hope this helps. On Sunday, November 15, 2015 at 6:51:30 AM UTC+2, Achu wrote: > > So in Matlab there's a pretty big difference in performance between > preallocating an array, and growing it in a loop with a=[a new_element]; > How big is the performance difference in Julia between preallocating and > using push! to grow the array in a loop? > > Thanks! > Achu >
