MathGroup Archive 1996

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

Search the Archive

Re: functional code


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[]