How to Check Computer Performance in Matlab?

In summary, to improve the performance of MATLAB, you can maximize the number of cores used, vectorize your code, and use the code profiler to identify areas of improvement. Additionally, preallocating your vectors to their final size can also speed up execution. In some cases, it may be possible to eliminate for-loops entirely and perform operations on vectors instead.
  • #1
beyondlight
65
0
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?
 
Physics news on Phys.org
  • #2
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.
 
  • #3
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 operativesystemWhen 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?
 
  • #4
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 accommodate 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
 
  • #5
kreil said:
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 accommodate 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.
 
  • #6
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/
 

FAQ: How to Check Computer Performance in Matlab?

How can I check the speed of my computer in Matlab?

To check the speed of your computer in Matlab, you can use the "tic" and "toc" commands. First, use the "tic" command to start a timer, then run the code you want to check the speed of. After the code has finished running, use the "toc" command to stop the timer and display the elapsed time in seconds.

What is the significance of checking computer speed in Matlab?

Checking the speed of your computer in Matlab can help you optimize and improve the performance of your code. It can also help you determine if your computer meets the minimum requirements for running certain programs or simulations.

Can I compare the speed of my computer to other computers?

Yes, you can compare the speed of your computer to other computers by running the same code on both machines and comparing the elapsed time. Keep in mind that the speed of a computer can be affected by various factors such as processor speed, memory, and operating system.

Is there a specific unit of measurement for computer speed in Matlab?

The unit of measurement for computer speed in Matlab is seconds. The elapsed time displayed using the "toc" command is in seconds.

How can I improve the speed of my computer in Matlab?

To improve the speed of your computer in Matlab, you can try optimizing your code by using vectorization, preallocation, and built-in functions. You can also consider upgrading your hardware, such as increasing your computer's memory or upgrading to a faster processor.

Similar threads

Replies
5
Views
1K
Replies
1
Views
2K
Replies
9
Views
3K
Replies
3
Views
1K
Replies
3
Views
2K
Replies
22
Views
3K
Replies
11
Views
3K
Back
Top