Check computer speed in Matlab

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?

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.

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.

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.

If you create a separate thread here and post your code we can also help you optimize that.

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.

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.

If you create a separate thread here and post your code we can also help you optimize that.

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.