Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Minor disappointment with Matlab
Date: Fri, 4 Sep 2009 22:23:02 +0000 (UTC)
Organization: Boeing
Lines: 42
Message-ID: <h7s406$m8j$1@fred.mathworks.com>
References: <25f0b0e8-fa6b-4283-81eb-d3bfebeb4a97@c37g2000yqi.googlegroups.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1252102982 22803 172.30.248.35 (4 Sep 2009 22:23:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 4 Sep 2009 22:23:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:568480


Dan <dnp037@yahoo.com> wrote in message <25f0b0e8-fa6b-4283-81eb-d3bfebeb4a97@c37g2000yqi.googlegroups.com>...
> The other day, because some of my simulation code was taking a long
> time to run, I spent some time trying to optimize the algorithm. This
> was wasted time, because when I finally got around to profiling the
> code, it seems that most of the time was spent in the Matlab "cross"
> function. I thought this was such a basic function that I did not look
> there for my problem. When I replaced the built-in function with my
> own, the simulation was sped up by about a factor of 30.
> 
> Here is some representative code to illustrate:
> 
> (execution times and percentages from "profile" on the right)
> 
> for k=1:100000
>     a = rand
> (1,3);    ..............................................................................
> 0.265s    2.7%
>     b = rand
> (1,3);    ..............................................................................
> 0.125s    1.3%
>     c = cross
> (a,b);  ..............................................................................
> 9.340s   94.2%
>     d = [ a(2)*b(3)-a(3)*b(2), a(3)*b(1)-a(1)*b(3), a(1)*b(2)-a(2)*b
> (1) ]; .........    0.172s    1.7% (same as "cross")
> end
> 
> 
> I recommend that you carefully consider using "cross" in calculation
> intensive code.
> 
> Regards,
> Dan

To be fair, the built-in cross is vectorized, does argument checking, and allows the dimension used for the cross to be specified. Nevertheless, your point is well taken ... the difference between inline code and the built-in function for the simple 1x3 case is striking (R2008a, WinXP):

>> tic;crosstest;toc  % built-in cross
Elapsed time is 11.885679 seconds.
>> tic;crosstest;toc  % inline cross
Elapsed time is 0.373162 seconds.

James Tursa