From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: please help optimize this ('find' is too slow)
Date: Sat, 6 Dec 2008 18:13:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 36
Message-ID: <ghefbe$ell$>
References: <gh46f4$pg2$> <gh7ror$eno$> <gh7vs8$fgq$> <gh9faq$fb4$> <gh9qmi$l5c$> <gh9ske$6he$> <gha1vj$850$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: 1228587182 15029 (6 Dec 2008 18:13:02 GMT)
NNTP-Posting-Date: Sat, 6 Dec 2008 18:13:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:505382

"Jayveer " <> wrote in message <gha1vj$850$>...
> lol, so silly of me! you are right! :) thanks loads dude! the processing time went down from 28s to 13s!
> however, considering right now i have a simulation running which initially said '10Hrs remaining', 13s is still quite slow coz the function is called 600k+ depending on the model being simulated. 
> you seem to have a pretty good understanding of code optimisation. have you got any more ideas how to make this even faster? i'm trying to bring this down to sort of 5s ish for this particular sample data.
> this one appears to be the slowest one now -takes 6.7s out of 13s
>         ny=nx(P(2,nx)>=min(NCy(c,:))&P(2,nx)<max(NCy(c,:)));
> any ideas?

  I notice a couple of other aspects of your code that you could change to possibly improve its timing efficiency, Jayveer.

  You are using the element-wise '&' operation rather than the short-circuit '&&' operation to do your AND-ing.  Using the latter would avoid wasting time on testing the second operand whenever the first were false.  That could make a noticeable difference on the line you mentioned:


  Also this line might conceivably be speeded up if you wrote

 temp = P(2,nx);
 ny=nx(temp>=Ly(c) && temp<Uy(c));

so that P(2,nx) and the lower and upper limits need be calculated only once per loop circuit.

  Finally, I don't think any of those tests you're doing for empty arrays are necessary, such as "if ~isempty(nx)" or "~isempty(ny)".  Matlab knows how to deal with an empty 'nx' in

 ny = nx(P(2,nx)>=...  

It just comes up with 'ny' also empty, which is appropriate.  You are wasting time on this testing where most of the cases are probably non-empty, at least for "~isempty(nx)" and probably for "~isempty(ny)".

  Of these various improvements, I would consider '&&' the most significant.

Roger Stafford