how to be as efficient as Mean[list] - efficient summing of function applied to list
- To: mathgroup at smc.vnet.net
- Subject: [mg28531] how to be as efficient as Mean[list] - efficient summing of function applied to list
- From: "Matthew D. Langston" <langston at SLAC.Stanford.EDU>
- Date: Wed, 25 Apr 2001 01:30:33 -0400 (EDT)
- Organization: Stanford Linear Accelerator Center
- Sender: owner-wri-mathgroup at wolfram.com
I have a one dimensional list of 28,900 data points. I want to apply a
function, called f[x_], to each element of this list and sum the resulting
list to obtain one number. For the following example, f[x_] := -Log[x]. My
naive idea was to do something like this:
Length[data]
28900
Apply[Plus,Map[f[#1]&,data]]//Timing
{32.136 Second,-69627.8}
However, this seemed to take to long when compared to other built-in
Mathematic functions that iterate over a list and perform a sum. For
example, the Mean function on the same data takes two orders of magnitude
less time than my function:
Mean[data]//Timing
{0.13 Second,-0.0296526}
Is the "Apply[Plus[...]]" method the right method to use for what I want to
do? Or, is it just that the time it takes my function f[x_] to perform one
calculation, when amortized over the whole 28,900 data points, just adds up
to a lot of time (32 seconds in my example).
Regards, Matt
--
Matthew D. Langston
SLD, Stanford Linear Accelerator Center
langston at SLAC.Stanford.EDU