Time Cost of Initializing Arrays in MATLAB

How much does initializing arrays actually improve performance in MATLAB? Quite a lot. However, sometimes it’s impossible or just a pain to pre-calculate the array size. I wanted to determine the sloppy-code-time tradeoff (in seconds). I setup a simple routine to build arrays either initialized or uninitialized and timed the execution.

Below are the results from building two arrays from 0 to 100 million elements with 1 million element steps. This was performed on a MacBook Pro 2.8 GHz Intel Core i7 with 16 GB 1600 MHz DDR3.

init_array_MATLAB.png

Take home: if an array is going to contain over ~1 million elements, it’s worth the time upfront to initialize it. You can try it yourself.