<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260122</link>
    <title>MATLAB Central Newsreader - Minor disappointment with Matlab</title>
    <description>Feed for thread: Minor disappointment with Matlab</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Fri, 04 Sep 2009 22:00:57 -0400</pubDate>
      <title>Minor disappointment with Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260122#678241</link>
      <author>Dan</author>
      <description>The other day, because some of my simulation code was taking a long&lt;br&gt;
time to run, I spent some time trying to optimize the algorithm. This&lt;br&gt;
was wasted time, because when I finally got around to profiling the&lt;br&gt;
code, it seems that most of the time was spent in the Matlab &quot;cross&quot;&lt;br&gt;
function. I thought this was such a basic function that I did not look&lt;br&gt;
there for my problem. When I replaced the built-in function with my&lt;br&gt;
own, the simulation was sped up by about a factor of 30.&lt;br&gt;
&lt;br&gt;
Here is some representative code to illustrate:&lt;br&gt;
&lt;br&gt;
(execution times and percentages from &quot;profile&quot; on the right)&lt;br&gt;
&lt;br&gt;
for k=1:100000&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a = rand&lt;br&gt;
(1,3);    ..............................................................................&lt;br&gt;
0.265s    2.7%&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b = rand&lt;br&gt;
(1,3);    ..............................................................................&lt;br&gt;
0.125s    1.3%&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c = cross&lt;br&gt;
(a,b);  ..............................................................................&lt;br&gt;
9.340s   94.2%&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;d = [ a(2)*b(3)-a(3)*b(2), a(3)*b(1)-a(1)*b(3), a(1)*b(2)-a(2)*b&lt;br&gt;
(1) ]; .........    0.172s    1.7% (same as &quot;cross&quot;)&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I recommend that you carefully consider using &quot;cross&quot; in calculation&lt;br&gt;
intensive code.&lt;br&gt;
&lt;br&gt;
Regards,&lt;br&gt;
Dan</description>
    </item>
    <item>
      <pubDate>Fri, 04 Sep 2009 22:13:02 -0400</pubDate>
      <title>Re: Minor disappointment with Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260122#678242</link>
      <author>Bruno Luong</author>
      <description>Dan &amp;lt;dnp037@yahoo.com&amp;gt; wrote in message &amp;lt;25f0b0e8-fa6b-4283-81eb-d3bfebeb4a97@c37g2000yqi.googlegroups.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I recommend that you carefully consider using &quot;cross&quot; in calculation&lt;br&gt;
&amp;gt; intensive code.&lt;br&gt;
&lt;br&gt;
Better still, vectorize the code. Most of Matlab functions has a lot of overhead because its design is not targeted for use within for-loop.&lt;br&gt;
&lt;br&gt;
You can check the source code of cross. It is not a *built-in* function, simply a stock mfile.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Fri, 04 Sep 2009 22:23:02 -0400</pubDate>
      <title>Re: Minor disappointment with Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260122#678243</link>
      <author>James Tursa</author>
      <description>Dan &amp;lt;dnp037@yahoo.com&amp;gt; wrote in message &amp;lt;25f0b0e8-fa6b-4283-81eb-d3bfebeb4a97@c37g2000yqi.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; The other day, because some of my simulation code was taking a long&lt;br&gt;
&amp;gt; time to run, I spent some time trying to optimize the algorithm. This&lt;br&gt;
&amp;gt; was wasted time, because when I finally got around to profiling the&lt;br&gt;
&amp;gt; code, it seems that most of the time was spent in the Matlab &quot;cross&quot;&lt;br&gt;
&amp;gt; function. I thought this was such a basic function that I did not look&lt;br&gt;
&amp;gt; there for my problem. When I replaced the built-in function with my&lt;br&gt;
&amp;gt; own, the simulation was sped up by about a factor of 30.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Here is some representative code to illustrate:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; (execution times and percentages from &quot;profile&quot; on the right)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for k=1:100000&lt;br&gt;
&amp;gt;     a = rand&lt;br&gt;
&amp;gt; (1,3);    ..............................................................................&lt;br&gt;
&amp;gt; 0.265s    2.7%&lt;br&gt;
&amp;gt;     b = rand&lt;br&gt;
&amp;gt; (1,3);    ..............................................................................&lt;br&gt;
&amp;gt; 0.125s    1.3%&lt;br&gt;
&amp;gt;     c = cross&lt;br&gt;
&amp;gt; (a,b);  ..............................................................................&lt;br&gt;
&amp;gt; 9.340s   94.2%&lt;br&gt;
&amp;gt;     d = [ a(2)*b(3)-a(3)*b(2), a(3)*b(1)-a(1)*b(3), a(1)*b(2)-a(2)*b&lt;br&gt;
&amp;gt; (1) ]; .........    0.172s    1.7% (same as &quot;cross&quot;)&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I recommend that you carefully consider using &quot;cross&quot; in calculation&lt;br&gt;
&amp;gt; intensive code.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Regards,&lt;br&gt;
&amp;gt; Dan&lt;br&gt;
&lt;br&gt;
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):&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; tic;crosstest;toc  % built-in cross&lt;br&gt;
Elapsed time is 11.885679 seconds.&lt;br&gt;
&amp;gt;&amp;gt; tic;crosstest;toc  % inline cross&lt;br&gt;
Elapsed time is 0.373162 seconds.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Sat, 05 Sep 2009 06:45:06 -0400</pubDate>
      <title>Re: Minor disappointment with Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260122#678270</link>
      <author>Bruno Luong</author>
      <description>function t&lt;br&gt;
&lt;br&gt;
n = 100000;&lt;br&gt;
A = rand(3,n);&lt;br&gt;
B = rand(3,n);&lt;br&gt;
&lt;br&gt;
tic&lt;br&gt;
for k=1:n&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a = A(:,k);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b = B(:,k);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c = cross(a,b);&lt;br&gt;
end&lt;br&gt;
toc % 7.201296 seconds.&lt;br&gt;
&lt;br&gt;
tic&lt;br&gt;
for k=1:n&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a = A(:,k);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b = B(:,k);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;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) ];&lt;br&gt;
end&lt;br&gt;
toc % 0.215036 seconds.&lt;br&gt;
&lt;br&gt;
tic&lt;br&gt;
C = cross(A,B);&lt;br&gt;
for k=1:n&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c = C(:,k);&lt;br&gt;
end % 0.078556 seconds.&lt;br&gt;
toc</description>
    </item>
    <item>
      <pubDate>Sat, 05 Sep 2009 10:35:42 -0400</pubDate>
      <title>Re: Minor disappointment with Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260122#678283</link>
      <author>Rune Allnor</author>
      <description>On 5 Sep, 00:23, &quot;James Tursa&quot;&lt;br&gt;
&amp;lt;aclassyguy_with_a_k_not_...@hotmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; Dan &amp;lt;dnp...@yahoo.com&amp;gt; wrote in message &amp;lt;25f0b0e8-fa6b-4283-81eb-d3bfebeb4...@c37g2000yqi.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; The other day, because some of my simulation code was taking a long&lt;br&gt;
&amp;gt; &amp;gt; time to run, I spent some time trying to optimize the algorithm.&lt;br&gt;
...&lt;br&gt;
&amp;gt; To be fair, the built-in cross is vectorized, does argument checking, and allows the dimension used for the cross to be specified.&lt;br&gt;
&lt;br&gt;
...all of which takes place at *run* time. A compiled&lt;br&gt;
language would do a lot (all?) of that at *compile* time.&lt;br&gt;
&lt;br&gt;
Rune</description>
    </item>
    <item>
      <pubDate>Tue, 08 Sep 2009 14:44:45 -0400</pubDate>
      <title>Re: Minor disappointment with Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260122#678753</link>
      <author>Dan</author>
      <description>On Sep 5, 1:45&#160;am, &quot;Bruno Luong&quot; &amp;lt;b.lu...@fogale.findmycountry&amp;gt; wrote:&lt;br&gt;
&amp;gt; function t&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; n = 100000;&lt;br&gt;
&amp;gt; A = rand(3,n);&lt;br&gt;
&amp;gt; B = rand(3,n);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; for k=1:n&lt;br&gt;
&amp;gt; &#160; &#160; a = A(:,k);&lt;br&gt;
&amp;gt; &#160; &#160; b = B(:,k);&lt;br&gt;
&amp;gt; &#160; &#160; c = cross(a,b);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; toc % 7.201296 seconds.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; for k=1:n&lt;br&gt;
&amp;gt; &#160; &#160; a = A(:,k);&lt;br&gt;
&amp;gt; &#160; &#160; b = B(:,k);&lt;br&gt;
&amp;gt; &#160; &#160; 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) ];&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; toc % 0.215036 seconds.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; C = cross(A,B);&lt;br&gt;
&amp;gt; for k=1:n&lt;br&gt;
&amp;gt; &#160; &#160; c = C(:,k);&lt;br&gt;
&amp;gt; end % 0.078556 seconds.&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&lt;br&gt;
I appreciate your response.&lt;br&gt;
&lt;br&gt;
However, you didn't check&lt;br&gt;
C = [ A(2,:).*B(3,:) - A(3,:).*B(2,:); A(3,:).*B(1,:)-A(1,:).*B(3,:); A&lt;br&gt;
(1,:).*B(2,:)-A(2,:).*B(1,:) ];&lt;br&gt;
&lt;br&gt;
which is always faster.&lt;br&gt;
&lt;br&gt;
Experienced users of Matlab know about the benefits of vectorization.&lt;br&gt;
Old time users have had to do the vectorization trade-off, when too&lt;br&gt;
much vectorization would cause the machine to use its virtual memory&lt;br&gt;
thus actually increasing run time because of the operating system&lt;br&gt;
memory swap. And, not every piece of code can be vectorized easily.&lt;br&gt;
&lt;br&gt;
My point was that Matlab is a wonderful tool, because of the ease and&lt;br&gt;
speed with which one can perform incredible amounts of calculations.&lt;br&gt;
The set of functions available to the user is amazing and powerful.&lt;br&gt;
When I use a &quot;built-in* &quot; function, I realize that it is most likely&lt;br&gt;
not the most efficient way to implement my desired function, but I'll&lt;br&gt;
accept the overhead because of the convenience.&lt;br&gt;
&lt;br&gt;
My only surprise was the factor of 30 difference using &quot;cross&quot; even&lt;br&gt;
for non vectorized code. If it had been a factor of 2 or 3, no big&lt;br&gt;
deal.  I only used &quot;cross&quot; because I was lazy and did not want to type&lt;br&gt;
out the expression. Now, maybe I have been naive for many years, and&lt;br&gt;
the same is true for most matrix/vector functions such as &quot;dot&quot;, or&lt;br&gt;
&quot;norm&quot;. Or maybe because the majority of my experiences have been with&lt;br&gt;
vectorized code, I had not noticed this phenomenon before. In any&lt;br&gt;
case, I shall be very careful when using Matlab functions when the&lt;br&gt;
code is not vectorized.&lt;br&gt;
&lt;br&gt;
Regards,&lt;br&gt;
Dan&lt;br&gt;
&lt;br&gt;
*Your clarification of built-in functions is a distinction without a&lt;br&gt;
practical difference.</description>
    </item>
    <item>
      <pubDate>Tue, 08 Sep 2009 15:07:02 -0400</pubDate>
      <title>Re: Minor disappointment with Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260122#678756</link>
      <author>Bruno Luong</author>
      <description>Where do you get the factor 30??? Here is my test result (2009B/Vista):&lt;br&gt;
&lt;br&gt;
function testcross&lt;br&gt;
&lt;br&gt;
n = 1e6;&lt;br&gt;
A = rand(3,n);&lt;br&gt;
B = rand(3,n);&lt;br&gt;
&lt;br&gt;
tic&lt;br&gt;
C = cross(A,B);&lt;br&gt;
toc % Elapsed time is 0.173915 seconds.&lt;br&gt;
&lt;br&gt;
tic&lt;br&gt;
C = [ A(2,:).*B(3,:) - A(3,:).*B(2,:); &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;A(3,:).*B(1,:)-A(1,:).*B(3,:); &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;A(1,:).*B(2,:)-A(2,:).*B(1,:) ];&lt;br&gt;
toc % Elapsed time is 0.168631 seconds.&lt;br&gt;
&lt;br&gt;
%%%%%&lt;br&gt;
&lt;br&gt;
If you look at the code of CROSS (it is *not* a built-in, there is a mfile-  type CROSS, then open it using right mouse button menu to open it), it does exactly the same calculation like your code plus few overheads.&lt;br&gt;
&lt;br&gt;
The factor 30 comes only when you use CROSS inside the for loop, which is a bad way of using it.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Tue, 08 Sep 2009 15:35:03 -0400</pubDate>
      <title>Re: Minor disappointment with Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260122#678758</link>
      <author>Bruno Luong</author>
      <description>Sorry Dan, I misread your post; We agree that the factor 30 is when put cross inside the loop. If you follow more closely the newsgroup, a lot of threads where we discuss about &quot;vectorization&quot; for the very reason you run into. It is critical and difficult - thus required a lot practice I must admit - to design the right code in Matlab so that the speed is not screwed up.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Tue, 08 Sep 2009 18:25:05 -0400</pubDate>
      <title>Re: Minor disappointment with Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260122#678801</link>
      <author>James Tursa</author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;h85rum$p3t$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The factor 30 comes only when you use CROSS inside the for loop, which is a bad way of using it.&lt;br&gt;
&lt;br&gt;
That statement is a bit too black-and-white for me. There can be several cases, such as simulations, where a cross product is needed in the formation of derivatives etc. that affect the very next iteration. It is impossible to vectorize the cross product calculations in this case over the entire simulation. That is precisely the case where a factor of 30 would not be tolerable and hand coding the calculations makes sense.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Tue, 08 Sep 2009 18:37:04 -0400</pubDate>
      <title>Re: Minor disappointment with Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260122#678805</link>
      <author>James Tursa</author>
      <description>Dan &amp;lt;dnp037@yahoo.com&amp;gt; wrote in message &amp;lt;37a033b3-1375-4593-b673-460ea2713401@x37g2000yqj.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;  I only used &quot;cross&quot; because I was lazy and did not want to type&lt;br&gt;
&amp;gt; out the expression. Now, maybe I have been naive for many years, and&lt;br&gt;
&amp;gt; the same is true for most matrix/vector functions such as &quot;dot&quot;, or&lt;br&gt;
&amp;gt; &quot;norm&quot;. Or maybe because the majority of my experiences have been with&lt;br&gt;
&amp;gt; vectorized code, I had not noticed this phenomenon before. In any&lt;br&gt;
&amp;gt; case, I shall be very careful when using Matlab functions when the&lt;br&gt;
&amp;gt; code is not vectorized.&lt;br&gt;
&lt;br&gt;
I wouldn't call using the cross function lazy ... if a function is already available I would likely make the same assumption that you did, that it was reasonably coded and not worth my time to hand-code myself.&lt;br&gt;
&lt;br&gt;
But dot, in particular, is a function I tend to avoid. In testing I have done, it uses a different algorithm (a simple loop) than a straight matrix multiply and is slower and somewhat less accurate. e.g.&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; a=rand(20000000,1)+rand(20000000,1)*i;&lt;br&gt;
&amp;gt;&amp;gt; b=rand(20000000,1)+rand(20000000,1)*i;&lt;br&gt;
&amp;gt;&amp;gt; format long&lt;br&gt;
&amp;gt;&amp;gt; a'*b&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;9.996446745265679e+006 -1.467960263183340e+003i&lt;br&gt;
&amp;gt;&amp;gt; dot(a,b)&lt;br&gt;
ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;9.996446745268498e+006 -1.467960262848279e+003i&lt;br&gt;
&amp;gt;&amp;gt; tic;a'*b;toc&lt;br&gt;
Elapsed time is 0.302599 seconds.&lt;br&gt;
&amp;gt;&amp;gt; tic;dot(a,b);toc&lt;br&gt;
Elapsed time is 0.774087 seconds.&lt;br&gt;
&lt;br&gt;
Although it is not obvious from what I have posted, I ran similar calculations against a 100 decimal digit accurate calculation and the a'*b result was the more accurate of the two. Admittedly, the difference is in the trailing bits that your calculations shouldn't depend on, but it probably takes away one of the only reasons I would use dot in the first place since it is slower than a matrix multiply. I would probably only use dot in multi-dimensional cases and even then only if I couldn't easily reformulate it into a matrix multiply.&lt;br&gt;
&lt;br&gt;
norm I have found to be pretty fast.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Tue, 08 Sep 2009 18:52:02 -0400</pubDate>
      <title>Re: Minor disappointment with Matlab</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/260122#678808</link>
      <author>James Tursa</author>
      <description>&quot;James Tursa&quot; &amp;lt;aclassyguy_with_a_k_not_a_c@hotmail.com&amp;gt; wrote in message &amp;lt;h8688g$bm8$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; tic;a'*b;toc&lt;br&gt;
&amp;gt; Elapsed time is 0.302599 seconds.&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; tic;dot(a,b);toc&lt;br&gt;
&amp;gt; Elapsed time is 0.774087 seconds.&lt;br&gt;
&lt;br&gt;
P.S. I should mention that the main reason for this speed difference seems to be that the BLAS calls behind the matrix multiply are multi-threaded (at least in later versions of MATLAB) whereas the simple loop used in the dot function is not. &lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
  </channel>
</rss>

