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