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.

Answers (1)

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

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?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 28 Feb 2015

Commented:

on 1 Mar 2015

Community Treasure Hunt

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

Start Hunting!