clear format compact % have slightly different n at every run (to avoid precompilation?) n = 100000 + randi([0 10]) % loop without preallocation tic(); for i = 1:n x(i) = i; end t_wo = toc() % loop with preallocation y = zeros(1, n); % preallocate a row vector tic(); for i = 1:n y(i) = i; end t_w = toc() % Speedup factor t_wo/t_w