MathGroup Archive 1996

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: functional code

  • To: mathgroup at smc.vnet.net
  • Subject: [mg4853] Re: functional code
  • From: rhall2 at umbc.edu (hall robert)
  • Date: Thu, 26 Sep 1996 22:42:14 -0400
  • Organization: University of Maryland, Baltimore County
  • Sender: owner-wri-mathgroup at wolfram.com

Thanks to David Lichtblau for explaining where the bottlenecks were. 
One further question; why does his code take twice as long to run as 
Hayes'? Does it have to do with constantly modifying 'result', rather 
than using Table[] to create the new list? The two functions are 
reproduced below.


hayes[row_] := Block[
   {f=1, l = Length[row]},
	
	Table[
		If[
			row[[f]]>row[[l]], 
			row[[f++]], 
			row[[l--]]
		],
		{l}
	]
]


lichtblau[input_List] := Module[
  {j=1, k=Length[input], result={}},
	
  While[j<=k,
	If [input[[j]] > input[[k]],
		  result = {result, input[[j]]}; j++,
		  result = {result, input[[k]]}; k--];
		];
  Flatten[result]
]

Join[] seems to be faster than Flatten[] on long lists. 

In[32]:=
   row = Table[Random[], {1000}];

In[38]:=
   Table[Flatten[{{a}, row}], {10}]; // Timing
Out[38]=
   {7.1 Second, Null}

In[39]:=
   Table[Join[{a}, row], {10}]; // Timing
Out[39]=
   {1.11667 Second, Null}

The difference isn't so pronounced on smaller lists. Can someone 
explain the difference in times?




-- 
Bob Hall            | "Know thyself? Absurd direction!
rhall2 at gl.umbc.edu  |  Bubbles bear no introspection."  -Khushhal Khan Khatak

==== [MESSAGE SEPARATOR] ====


  • Prev by Date: Different Plots in the Same Window?
  • Next by Date: Help - Compile[] problems
  • Previous by thread: functional code
  • Next by thread: [Q] ListPlotVectorField[]