Best Practices for Efficiency

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일

0 개 추천

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

Yes, I'm sure it's these three lines. I've even just added the code below at the beginning of my script and get a similar problem (over 10 million repetitions, the time for each line is 25 seconds)
for i = 1:10000000
parameter1 = 8;
parameter2 = 3;
parameter3 = 1;
end
How can I know if i'm using the parallel toolbox?
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.
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?

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 2월 28일

댓글:

2015년 3월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by