Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!x37g2000yqj.googlegroups.com!not-for-mail
From: Dan <dnp037@yahoo.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Minor disappointment with Matlab
Date: Tue, 8 Sep 2009 07:44:45 -0700 (PDT)
Organization: http://groups.google.com
Lines: 67
Message-ID: <37a033b3-1375-4593-b673-460ea2713401@x37g2000yqj.googlegroups.com>
References: <25f0b0e8-fa6b-4283-81eb-d3bfebeb4a97@c37g2000yqi.googlegroups.com> 
	<h7s406$m8j$1@fred.mathworks.com> <h7t1dh$41j$1@fred.mathworks.com>
NNTP-Posting-Host: 199.46.240.168
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1252421085 12780 127.0.0.1 (8 Sep 2009 14:44:45 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Tue, 8 Sep 2009 14:44:45 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: x37g2000yqj.googlegroups.com; posting-host=199.46.240.168; 
	posting-account=A4bkYwkAAACb_Gvc_qdps_d-fzN6tydb
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET 
	CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1),gzip(gfe),gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:568995


On Sep 5, 1:45 am, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> function t
>
> n = 100000;
> A = rand(3,n);
> B = rand(3,n);
>
> tic
> for k=1:n
>     a = A(:,k);
>     b = B(:,k);
>     c = cross(a,b);
> end
> toc % 7.201296 seconds.
>
> tic
> for k=1:n
>     a = A(:,k);
>     b = B(:,k);
>     c = [ a(2)*b(3)-a(3)*b(2) a(3)*b(1)-a(1)*b(3) a(1)*b(2)-a(2)*b(1) ];
> end
> toc % 0.215036 seconds.
>
> tic
> C = cross(A,B);
> for k=1:n
>     c = C(:,k);
> end % 0.078556 seconds.
> toc

I appreciate your response.

However, you didn't check
C = [ A(2,:).*B(3,:) - A(3,:).*B(2,:); A(3,:).*B(1,:)-A(1,:).*B(3,:); A
(1,:).*B(2,:)-A(2,:).*B(1,:) ];

which is always faster.

Experienced users of Matlab know about the benefits of vectorization.
Old time users have had to do the vectorization trade-off, when too
much vectorization would cause the machine to use its virtual memory
thus actually increasing run time because of the operating system
memory swap. And, not every piece of code can be vectorized easily.

My point was that Matlab is a wonderful tool, because of the ease and
speed with which one can perform incredible amounts of calculations.
The set of functions available to the user is amazing and powerful.
When I use a "built-in* " function, I realize that it is most likely
not the most efficient way to implement my desired function, but I'll
accept the overhead because of the convenience.

My only surprise was the factor of 30 difference using "cross" even
for non vectorized code. If it had been a factor of 2 or 3, no big
deal.  I only used "cross" because I was lazy and did not want to type
out the expression. Now, maybe I have been naive for many years, and
the same is true for most matrix/vector functions such as "dot", or
"norm". Or maybe because the majority of my experiences have been with
vectorized code, I had not noticed this phenomenon before. In any
case, I shall be very careful when using Matlab functions when the
code is not vectorized.

Regards,
Dan

*Your clarification of built-in functions is a distinction without a
practical difference.