Path: news.mathworks.com!not-for-mail
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$1@fred.mathworks.com>
References: <gh46f4$pg2$1@fred.mathworks.com> <gh7ror$eno$1@fred.mathworks.com> <gh7vs8$fgq$1@fred.mathworks.com> <gh9faq$fb4$1@fred.mathworks.com> <gh9qmi$l5c$1@fred.mathworks.com> <gh9ske$6he$1@fred.mathworks.com> <gha1vj$850$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1228587182 15029 172.30.248.37 (6 Dec 2008 18:13:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 6 Dec 2008 18:13:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:505382

"Jayveer " <jveer@jveer.com> wrote in message <gha1vj$850$1@fred.mathworks.com>...
> 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:

 ny=nx(P(2,nx)>=min(NCy(c,:))&P(2,nx)<max(NCy(c,:)))

  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