How to Check Computer Performance in Matlab?

  • Context: MATLAB 
  • Thread starter Thread starter beyondlight
  • Start date Start date
  • Tags Tags
    Computer Matlab Speed
Click For Summary

Discussion Overview

The discussion revolves around assessing and improving computer performance when running MATLAB functions that require extensive computations. Participants explore various factors affecting performance, including hardware specifications, coding practices, and optimization techniques.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant inquires about methods to check computer performance in MATLAB and seeks example files for benchmarking.
  • Another participant notes that MATLAB's performance is influenced by the processor type and number of cores, suggesting the use of the command maxNumCompThreads to check core availability.
  • Concerns are raised about the efficiency of code that uses loops, with suggestions to vectorize operations to improve performance.
  • A participant shares their computer specifications and notes a discrepancy between the number of cores reported by MATLAB and the processor's capabilities, questioning the potential for optimization.
  • Discussion includes the importance of preallocating vectors to avoid performance degradation due to memory reallocation during loop iterations.
  • Examples are provided on how to eliminate loops in MATLAB by using elementwise operations and logical indexing to enhance computational efficiency.
  • Some participants express uncertainty about how to optimize their specific code without further details and suggest creating a separate thread for code review.

Areas of Agreement / Disagreement

Participants generally agree on the importance of optimizing code and utilizing available processor resources, but there remains uncertainty regarding specific optimization strategies and the impact of hardware on performance. No consensus is reached on the best approach to eliminate loops or the exact performance gains achievable.

Contextual Notes

Limitations include the dependency on specific hardware configurations, the need for further information on individual code implementations, and unresolved questions about the exact performance implications of various coding practices.

beyondlight
Messages
64
Reaction score
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
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 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?
 
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
 
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.
 
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/
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
5
Views
3K