Best Practices for Efficiency

조회 수: 1 (최근 30일)
Groat
Groat 2015년 2월 28일
댓글: Stephen23 2015년 3월 1일
Hi,
I have a function that's run around 10 million times, so I want to make sure I'm doing things as efficiently as possible.
I use three parameters which are constants. Currently within my function I have:
parameter1 = 3;
parameter2 = 4;
parameter3 = 39;
These parameters are not used anywhere else other than this function. However, when looking at profiler, each line is taking up a considerable time.
Is there a more efficient method to utilise these parameters in the function? For example, global variables, etc?
Any help is much appreciated.

답변 (1개)

Image Analyst
Image Analyst 2015년 2월 28일
Here's my time for the 3 lines:
Elapsed time is 0.000004 seconds.
Perhaps there's some setup time involved because you're using the parallel toolbox. Are you sure it's those 3 lines that's taking up all the time and not some other lines of code?
  댓글 수: 3
Image Analyst
Image Analyst 2015년 2월 28일
You're not. You'd need to use parfor instead of for. Type ver on the command line to see if you have the toolbox.
I don't have the toolbox but when I run the code
tic
for i = 1:10000000
parameter1 = 8;
parameter2 = 3;
parameter3 = 1;
end
toc
I get this:
Elapsed time is 0.028298 seconds.
Far, far from the 75 seconds or more that it's taking you. I think there's something you're not telling us or some code you're not sharing with us.
Stephen23
Stephen23 2015년 3월 1일
When I run that loop (2010b on an old laptop) I get
Elapsed time is 0.114191 seconds.
Are the variables declared to be persistent or global?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by