Best Practices for Efficiency

2 views (last 30 days)
Groat
Groat on 28 Feb 2015
Commented: Stephen23 on 1 Mar 2015
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.

Answers (1)

Image Analyst
Image Analyst on 28 Feb 2015
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 Comments
Image Analyst
Image Analyst on 28 Feb 2015
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 on 1 Mar 2015
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?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!