Re: how to be as efficient as Mean[list] - efficient summing of function applied to list
- To: mathgroup at smc.vnet.net
- Subject: [mg28539] Re: how to be as efficient as Mean[list] - efficient summing of function applied to list
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Wed, 25 Apr 2001 19:21:47 -0400 (EDT)
- References: <9c5o3v$iv1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
First: be as direct as possible with the function:
data= Table[Random[Real,{.1,10}],{30000}];
f[x_] := -Log[x];
Apply[Plus,Map[f[#1]&,data]]//Timing
{6.15 Second,-40363.9}
Apply[Plus,Map[f,data]]//Timing
{5.11 Second,-40363.9}
Apply[Plus,Map[-Log[#]&,data]]//Timing
{0.55 Second,-40363.9}
Second, use the same method as the package Statistics`DescriptiveStatistics`
for summing (use Tr}
Tr[Map[-Log[#]&,data]]//Timing
{0.22 Second,-40363.9}
--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
"Matthew D. Langston" <langston at SLAC.Stanford.EDU> wrote in message
news:9c5o3v$iv1 at smc.vnet.net...
> 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
>
>