This is the first post in the series of Making Matlab Code Faster series.
As you might have realized already, getting stuff somehow to get to work, might be a fairly simple task with Matlab. But why stop there?
Why not make the necessary changes to see if your code can perform better. There are few things you can do towards this goal. Preallocation is just one of them.
Let's look at an example:
a=rand(1000000,1); %Declare a random matrix with 1 million elements
We can see that initially array 'b' is single element matrix. But as time goes on, we keep incrementing the size of the array.
This is not an efficient approach. Repeatedly resizing arrays often requires MATLAB to spend extra time looking for larger contiguous blocks of memory, and then moving the array into those blocks.
So how can we solve this issue? We can Preallocate the space for 'b'.
You would say that, you don't know what the end size of 'b' would be. In such a case, just preallocate anyway with the maximum size possible and then at the end of processing just remove the unused space.
See the solution here:
For the first approach without Preallocation , it takes 66.5 msec. while,
for the second apprach with Preallocation it only took 36.5 msec.
Almost 50% efficiency with just a single step!
As you might have realized already, getting stuff somehow to get to work, might be a fairly simple task with Matlab. But why stop there?
Why not make the necessary changes to see if your code can perform better. There are few things you can do towards this goal. Preallocation is just one of them.
Let's look at an example:
a=rand(1000000,1); %Declare a random matrix with 1 million elements
b=0;
j=1;
for i=1:size(a,1) %run the loop for the all the elements in 'a'.
if(a(i) < 0.5) %if the current element is less than 0.5 save it in an array 'b'
b(j)=a(i);
j=j+1; %increment j to save next element in the next location in 'b'
end
end
We can see that initially array 'b' is single element matrix. But as time goes on, we keep incrementing the size of the array.
This is not an efficient approach. Repeatedly resizing arrays often requires MATLAB to spend extra time looking for larger contiguous blocks of memory, and then moving the array into those blocks.
So how can we solve this issue? We can Preallocate the space for 'b'.
You would say that, you don't know what the end size of 'b' would be. In such a case, just preallocate anyway with the maximum size possible and then at the end of processing just remove the unused space.
See the solution here:
b=zeros(size(a)); %Preallocate with the maximum size possible for 'b'.
j=1;
for i=1:size(a,1)
if(a(i) < 0.5)
b(j)=a(i);
j=j+1;
end
end
b(j:end)=[]; %remove the unused elements at the end.
Using the tic-toc timers, I measured the average time for these two code snippets. The result is:For the first approach without Preallocation , it takes 66.5 msec. while,
for the second apprach with Preallocation it only took 36.5 msec.
Almost 50% efficiency with just a single step!
Comments
Post a Comment