0

I have a function which in total takes 34s and I want to speed this up. The 2 slowest functions are:

1) I have a very simple function file:

function [x] = percentChange(startPoint, currentPoint)
x = ( (currentPoint-startPoint)/abs(startPoint) )*100.00;

where currentPoint and startPoint are just integers. During my main function I call this function 1.114.239 times (which takes my computer 13.364s). Can I make this any faster?

2) Another part of my function which takes quite a while is the plotting of 1934 lines. Currently, the plotting is done as follows:

for i=1:size(patternPlot,1)
    hold all
    plot(xplot,patternPlot(i,:)); 
end

'patternPlot' stores the vectors i want to plot (xplot is just the vector 1:30). Can I speed this up in any way?

Thanks in advance,

J

user84112
  • 5
  • 3

1 Answers1

0

In 1): remove outer parentheses in second line. Probably won't gain speed; just for clarity.

Do you really have to call that function so many times, each with a number? Can't you do (currentPoint-startPoint)./abs(startPoint)*100.00 where currentPointand startPoint are vectors?

In 2): instead of the loop do a single, "vectorized plot": plot(xplot,patternPlot), or better plot(patternPlot.'). That will plot everything in a single step.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • Thanks! The speed of number 2 is reduced from 7s to 0.498975s and number 1 seems to be 1s faster (although i'm not sure if the removal of those brackets has anything to do with it) – user84112 Nov 15 '13 at 15:55
  • @user84112 Welcome! Well, that's better than nothing. Maybe someone will come up with further reductions – Luis Mendo Nov 15 '13 at 16:03