Re: Timing runs for the last part of my previous post
- To: mathgroup at smc.vnet.net
- Subject: [mg62075] Re: Timing runs for the last part of my previous post
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Thu, 10 Nov 2005 02:52:05 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
On 11/9/05 at 3:46 AM, anonmous69 at netscape.net (Matt) wrote: >After I posted my earlier message (the one entitled "'Good' or >'Proper' Mathematica coding habits question"), I decided to try >some timings for the last code sample I had a question on (the one >trying to extract all sublists where each element of a sublist had >to be negative). Here's what I found: >This table was generated, then used for all the approaches: values >= Table[{Random[Real, {-2, 2}] i, Random[Real, {-2, 2}] i}, {i, 1, >2000}]; >Approach #1: >ClearAll[testFuncOne]; >testFuncOne[] := Module[{negElems = {}}, > (If[Negative[#[[1]]] && > Negative[#[[2]]], negElems = {negElems, #}]) & /@ values; > StandardForm[Partition[Flatten[negElems], 2]]; > ]; <snip> >The results obtained were 13.625, 12.812, and 19.11 Seconds. So, >it appears that of the methods I tried, that Approach 2 is >marginally better than Approach 1, and both Approach 1 and Approach >2 are better than Approach 3. Is it correct to assume from this >that Fold will almost always be better than Map, given that other >potential variants are kept similar? Or, because the difference is >so small, that for most applications, I should go with whatever >approach is quicker to 'code up'? FWIW, on my machine In[74]:=Timing[Do[testFuncOne[], {1000}]][[1]] Out[74]=55.072*Second But In[83]:= Clear[func] func[values_] := Select[values, Sign[#1] == {-1, -1} &] In[84]:= Timing[Do[func[values], {1000}]][[1]] Out[84]=26.678*Second a factor of 2 improvement. And I would argue, using Select results in simpler more readable code. Somewhat faster using Cases and pattern matching is: In85:= patternFunc[values_] := Cases[values,{_?(#<0&), _?(#<0&)}] In[86]:= Timing[Do[patternFunc[values], {1000}]; ][[1]] Out[64]=23.477*Second -- To reply via email subtract one hundred and four