MathGroup Archive 2005

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

Search the Archive

Re: Timing runs for the last part of my previous post

  • To: mathgroup at
  • Subject: [mg62044] Re: Timing runs for the last part of my previous post
  • From: albert <awnl at>
  • Date: Thu, 10 Nov 2005 02:50:26 -0500 (EST)
  • References: <dksech$hdb$>
  • Sender: owner-wri-mathgroup at

Hi Matt,

> 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.

there are other possibilities, one which I think is rather clear about what
it does, and also quite efficient is the following:

testFuncFour[] := Select[values, Negative[#[[1]]] && Negative[#[[2]]] &]

here are the runtimes on my machine:

Timing[Do[testFuncOne[], {10^3}]][[1]]
17.218382999999996` Second
Timing[Do[testFuncTwo[], {10^3}]][[1]]
16.271525999999998` Second
Timing[Do[testFuncThree[], {10^3}]][[1]]
26.428982999999995` Second
Timing[Do[testFuncFour[], {10^3}]][[1]]
10.093465000000007` Second

When programming in Mathematica I think the best thing is to try to write
code that states as clear as possible what you are trying to achieve.
Always try to find a function in the helpbrowser that's purpose is as close
as possible to what you want to achieve. If you are searching for maximum
performance, you will find that this approach often gives you reasonable
performance, but not always the best possible.
>  Is it correct to assume from this that Fold will
> almost always be better than Map, given that other potential variants
> are kept similar?  

No, I think it depends a lot on the particular problem. Map is quite often a
good candidate for clean and fast solutions, but be aware that it always
returns a list which it has to construct. So if you are throwing that away
as in this case, you better use Scan, which does not construct the result
in the first place. Check the following variant:

testFuncFive[] := Module[{negElems = {}},
      Scan[(If[Negative[#[[1]]] && Negative[#[[2]]], 
              negElems = {negElems, #}]) &, values];
      StandardForm[Partition[Flatten[negElems], 2]];];

Timing[Do[testFuncFive[], {10^3}]][[1]]
16.167541` Second

and you will see that it is almost exactly as fast as the Fold-approach.

> Or, because the difference is so small, that for 
> most applications, I should go with whatever approach is quicker to
> 'code up'?

I would definitly go for the version which is quick to code up and easy to
understand (and debug). Analysing why one way is more efficient than
another in Mathematica is often quite hard and needs a good understanding
of many parts of Mathematica...


  • Prev by Date: Course Description:,M50: An Introduction to Mathematica in the Classroom not offered online
  • Next by Date: Re: 'Good' or 'Proper' Mathematica coding habits question
  • Previous by thread: Re: Timing runs for the last part of my previous post
  • Next by thread: Re: Timing runs for the last part of my previous post