Re: Re: functional code
- To: mathgroup at smc.vnet.net
- Subject: [mg4850] Re: [mg4814] Re: functional code
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Thu, 26 Sep 1996 22:42:13 -0400
- Sender: owner-wri-mathgroup at wolfram.com
At the risk of producing more C-like code (!), here is a further speed up together with a speed comparison with my code used in Bob Hall's list --attached. The omission of the n, which accounts for 2/3 of the sped-up, was posted a few hours ago. The message seems to be: be aware of all of Mma's programming styles and make use of special features of the problem. (*my original code called "hayes" in Robert Hall's list -- attached*) try1[row_List] := Block[{f=1,l = Length[row]}, Table[If[row[[f]]>row[[l]], row[[f++]], row[[l--]]],{n,l}] ] (*latest code*) try2[row_List] := Block[{f=1,l = Length[row]}, row[[Table[If[row[[f]]>row[[l]], f++, l--], {l}]]] ] (*timings*) testrow = Table[Random[],{5000}]; (ans1 = try1[testrow]);//Timing {4.98333 Second, Null} (ans2 = try2[testrow]);//Timing {3.55 Second, Null} (*check*) ans1==ans2 True Allan Hayes hay at haystack.demon.co.uk ************** Bob Hall's posting of timings Here's the times for the various solutions to Richard Gaylord's problem: In[90]:= original[row] Out[90]= {0.7 Second, Null} In[92]:= gaylord[row] Out[92]= {2.51667 Second, Null} In[94]:= hall[row] Out[94]= {3.63333 Second, Null} In[96]:= abbott1[row] Out[96]= {1.03333 Second, Null} In[98]:= abbott2[row] Out[98]= {1.13333 Second, Null} In[100]:= hayes[row] Out[100]= {0.116667 Second, Null} The most obvious algorithm, using iteration rather than recursion (original, hayes, and the 2 abbotts,) was the fastest. The most C-like code was the fastest (hayes.) My code was the least intuitive, probably the hardest to debug, and the slowest. Oh well.... "I can't hope to be First[], so let me be Last[]." -Auden, The Age of Anxiety -- Bob Hall | "Know thyself? Absurd direction! rhall2 at gl.umbc.edu | Bubbles bear no introspection." -Khushhal Khan Khatak ==== [MESSAGE SEPARATOR] ====