Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2001
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2001

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

Search the Archive

Re: Re: how to be as efficient as Mean[list] - efficient summing of function applied to list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg28580] Re: [mg28539] Re: how to be as efficient as Mean[list] - efficient summing of function applied to list
  • From: BobHanlon at aol.com
  • Date: Sat, 28 Apr 2001 21:36:05 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

You can get a slight improvement by noting that Log is Listable (which also 
makes f Listable).  

Using 300,000 data points:

data= Table[Random[Real,{.1,10}],{300000}];

Tr[Map[-Log[#]&,data]]//Timing

{0.9833333333335759*Second, 
  -404534.38111083925}

Tr[-Log[data]]//Timing

{0.8499999999985448*Second, 
  -404534.38111083925}

Bob Hanlon

In a message dated 2001/4/25 7:40:50 PM, hay at haystack.demon.co.uk writes:

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


  • Prev by Date: Re: question of formatting with PaddedForm
  • Next by Date: RE: Extended ListContourPlot and ContourShading
  • Previous by thread: Re: how to be as efficient as Mean[list] - efficient summing of function applied to list
  • Next by thread: Help with a simple problem.