A very well known piece of knowledge is that Matlab is bad at for loop. But you might ask: why?

Matlab is an interpreted language. This means that it will take time to read each line, more than it would for a compiled C Code. As a result these following lines of code are very different, even if the end result is the same.

x=ones(10000,1); x=x+x;

and :

x=ones(10000,1); for i=1:10000 x(i)=x(i)+x(i); end

In the second version of the code, Matlab has to interpret the line 3 at each loop iteration. So, in essence, we are asking Matlab to actually read **20002 lines of code** (one for line 1 and 2, and 10000 lines for both line 3 and 4). I guess the interpreting time will depend on the particular execution you ask and your machine, but on my machine, this reading time is about 5 microsecond per line.

So if you do the math, the difference is 10 microseconds reading time for code 1 and 100 ms reading time for code version 2. That is quite a difference!

This is why, it is highly recommended to **vectorize **your code whenever possible. Now keep in mind, that you can still use for loop. Actually for loop are often much easier to understand and read by humans than vectorize code, but you should make sure that the for loop is not looping over too many indices, otherwise your reading time skyrocket!

Â Now, if you do a good job, Matlab is quite fast (actually, if you don’t seriously code (**very** seriously) your C code, Matlab is faster than C at any matrix algebra) . Please execute this :

x=ones(10000,1); tic x=x+x; timeSpent=toc/10000

tic and toc function are convenients and quick ways to look at how much time is spent between them. On my computer, this gives me 1.5 nanoseconds. This time is the time per operation (in this case a +) given that you need to sum 10000 elements of double precision. My machine is a 2.8 GhZ CPU so this operation time is down to 4-5 CPU cycles. In other words, we are damn close to the limit you can do. At some point, I will take the time to examine and explain here how these processes are done at the CPU level but that is for another day…

If you follow this train of thoughts, you will see that if a line is executed on less than about a couple thousand objects (so that the total number of operation sum up to the microsecond range), the actual time spent is mainly READING it, ie 5 microseconds on my computer. Now this can vary from computer to computer but that is the order of magnitude.

Thanks for this really simple, but useful tip! I have to say I was taught to work with for loops during my Bachelors and this bad habit has continued till my PhD (I don’t use Matlab as my main PhD tool, but right now I’m writing a few programmes to run image analysis ðŸ™‚ ) I bet there are a couple of for loops I could totally vectorize! My professor probably won’t be too happy about me spending time on Matlab instead of just manually analyzing the images – but coding makes me happy ðŸ™‚ Thanks for all your great tips!

Why did you divide by 10000?

To show the time needed for one sum of 2 numbers

really helpful , tnx .

Pingback: MatLab | New ThinKing