# Check computer speed in Matlab

• MATLAB

## Main Question or Discussion Point

Hi. I have written some functions in Matlab and I would like to use them, the problem is that they require a lot of computations and hence takes a lot of time to calculate everything. I suspect that my computer may be slower than it should. How can I check the performance of my computer speed in Matlab?

Is there any example file that I can use as a benchmark?

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
kreil
Gold Member
Most of the basic operations are automatically multithreaded, so the performance of MATLAB is closely tied to:

1. The type of processor you have. Generally more cores = quicker execution. You can type maxNumCompThreads at the command line to see how many cores MATLAB sees/can use. Sometimes this isn't set to the maximum value, so increasing it results in faster run time.
2. Whether your code is vectorized. If you have a ton of loops and bad coding in the functions, they can run significantly slower than if you were to vectorize the code. (Vectorizing code refers to completing an operation on a vector of values instead of looping through single values, thus avoiding for-loops).

You can further drill down the performance using the code profiler, which allows you to see where MATLAB is spending its time during the calculation.

If you really wanted to benchmark, you could write a test file and have a friend execute it. But ultimately any timing difference can probably be explained by the type of processor.

Good post!

I will try too look into all of the alternatives especially try to maximise the number of cores used since i have 4 of them! Also i have a vector that changes size for every loop-iteration. I think that is a bad way to write code but I don't know how to do it differently.

Computer specifications:

Lenovo Laptop
i7-3632QM Processor
CPU 2.2 GHz
8 GB RAM
64-bits operativesystem

When i ran maxNumCompThreads i got 4. But on intels website they say i have 8 threads on my processor. So this means i could optimize by a factor of 2?

kreil
Gold Member
The speedup isn't always linear with more cores, but setting maxNumCompThreads to 8 should definitely help. Your computer specs look decent so they shouldn't really pose a problem unless your program is quite large.

Also i have a vector that changes size for every loop-iteration. I think that is a bad way to write code but I don't know how to do it differently.
The reason this slows down execution is that your computer constantly has to reallocate memory as the array gets larger. Compare this to a dinner party reservation. If you make a reservation for 5 people, then shortly after dinner starts a 6th person shows up, the restaurant has to accomodate them. Then a 7th shows up. Then an 8th, and a 9th. Soon the restaurant needs a bigger table for the party of 9, and they really would have appreciated that information up front!

But seriously, the way to avoid this is by preallocating the vector to its final size. So if, after the loop, the vector is 1x100, then all you need to do is make a 1x100 vector of zeros before the loop. Then the loop changes the values already present in the vector instead of changing the size. With the previous example, this is like making a reservation for 5 people, then 3 people cancel and 3 different people show up, but the total remains 5. The restaurant could care less in this case since the reservation was accurate. With MATLAB, it's computationally cheaper to change an existing value in an array than to constantly reevaluate how big the array needs to be.

That said, it's possible you can eliminate the loop entirely and just do operations on vectors.

Hope this helps

The speedup isn't always linear with more cores, but setting maxNumCompThreads to 8 should definitely help. Your computer specs look decent so they shouldn't really pose a problem unless your program is quite large.

The reason this slows down execution is that your computer constantly has to reallocate memory as the array gets larger. Compare this to a dinner party reservation. If you make a reservation for 5 people, then shortly after dinner starts a 6th person shows up, the restaurant has to accomodate them. Then a 7th shows up. Then an 8th, and a 9th. Soon the restaurant needs a bigger table for the party of 9, and they really would have appreciated that information up front!

But seriously, the way to avoid this is by preallocating the vector to its final size. So if, after the loop, the vector is 1x100, then all you need to do is make a 1x100 vector of zeros before the loop. Then the loop changes the values already present in the vector instead of changing the size. With the previous example, this is like making a reservation for 5 people, then 3 people cancel and 3 different people show up, but the total remains 5. The restaurant could care less in this case since the reservation was accurate. With MATLAB, it's computationally cheaper to change an existing value in an array than to constantly reevaluate how big the array needs to be.

That said, it's possible you can eliminate the loop entirely and just do operations on vectors.

Hope this helps
I can starta a thread soon with my code in and get some help with optimizing it. I think it's important to know how to write code without for loops since they are time consuming and it seems the most basic way to optimize stuff in MATLAB.

The problem is that i do not know how many local max points each image has since they are blurred with different filters. And it also depends on which image is used.

kreil
Gold Member
I would need more info to help, but here are some general examples of eliminating a for-loop in MATLAB.

1. Consider the following loop, which loops through the values in two vectors and multiplies them.

Code:
x = 1:10;
y = 100:110:
for i = 1:length(x)
z(i) = x(i)*y(i);
end
This is a dramatic example to illustrate the point, since you can replace that loop by just using the elementwise multiplication operator, .*

Code:
x = 1:10;
y = 100:110:
z = x.*y;

2. This is a slightly more complex example that uses logical indexing to replace the use of loops. This loop calculates values based on conditions.

Code:
A=rand(100,1);
B=rand(100,1);
for i=1:100
if B(i)>0.5
C(i)=A(i)^2;
else
C(i)=exp(B(i));
end
end
You can vectorize this with the following. Note the use of D and ~D to pick out the values that are greater than or less than 0.5.

Code:
A=rand(100,1);
B=rand(100,1);
D=(B>0.5);
C=D.*(A.^2)+(~D).*exp(B);
Reference: http://www.matlabtips.com/the-art-of-vectorizing-part-1/