MathGroup Archive 2004

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

Search the Archive

Re: Re: Question on Compile[]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49799] Re: [mg49786] Re: [mg49747] Question on Compile[]
  • From: DrBob <drbob at bigfoot.com>
  • Date: Sun, 1 Aug 2004 04:10:02 -0400 (EDT)
  • References: <200407301002.GAA26336@smc.vnet.net> <200407310714.DAA12055@smc.vnet.net> <opsb0onfdbiz9bcq@monster.cox-internet.com> <FFB63DC0-E331-11D8-B488-000A95B4967A@mimuw.edu.pl>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

Ah! I did see another post from Andrzej, but somehow I got confused.

I'm really shocked that a nineteen-degree polynomial raised to thousands of different powers can be competitive with the much simpler Fold, multiplying with z=1/(1+r). But, I suppose, that's the power of Compile! So I compiled my Fold application:

mukasa[cashFlow_, rate_] := Module[{nvec = Range[Length[
   cashFlow]]}, Total[cashFlow/(1. + rate)^nvec]]
andrzej = Compile[{{cashFlow, _Real, 1}, {rate, _Real}}, Module[{nvec =
               Range[Length[cashFlow]]}, Total[
cashFlow*(-rate^19 +
      rate^18 - rate^17 + rate^16 - rate^15 + rate^14 - rate^13 +
         rate^12 - rate^11 + rate^10 -
       rate^9 + rate^8 -
          rate^7 + rate^6 - rate^5 + rate^4 - rate^3 + rate^2 - rate + 1)^
         nvec]]];
treat[cc_List, r_] :=
             Block[{z = 1/(1 + r)}, z*Fold[#1*z + #2 &, 0, Reverse[cc]]]
bobby = Compile[{{cc, _Real, 1}, {r, _Real}}, Block[{z = 1/(1 +
                   r)}, z*Fold[#1*z + #2 &, 0, Reverse[cc]]]];
cashFlow[n_] := RandomArray[CauchyDistribution[0, 1], {n}];

cc=cashFlow[10^6];
r=0.1;
Timing[mukasa[cc,r]]
Timing[andrzej[cc,r]]
Timing[treat[cc,r]]
Timing[bobby[cc,r]]

{19.469 Second,31.3715}

{0.375 Second,31.3715}

{0.313 Second,31.3715}

{0.234 Second,31.3715}

These are ridiculous problem sizes considering the application, of course; at reasonable sizes the last three methods are virtually indistinguishable.

So I'd use Fold because it's simpler, I think, and probably wouldn't bother with Compile.

Bobby

On Sat, 31 Jul 2004 22:41:29 +0200, Andrzej Kozlowski <akoz at mimuw.edu.pl> wrote:

>
> On 31 Jul 2004, at 21:34, DrBob wrote:
>
>> mukasa[cashFlow_,rate_]:=Module[{nvec=Range[Length[cashFlow]]},
>>     Total[cashFlow/(1.0+rate)^nvec]]
>> andrzej=Compile[{{cashFlow,_Real,1},{rate,_Real}},Module[{
>>
>> nvec=Range[Length[cashFlow]]},Total[cashFlow/(1.0+rate)^nvec]]];
>> treat[cc_List,r_]:=Block[{z=1/(1+r)},
>>     z Fold[#1 z+#2&,0,Reverse@cc]
>>     ]
>> cashFlow[n_]:=Table[Random[],{n}];
>>
>> cc=cashFlow[100000];
>> r=0.1;
>> Timing@mukasa[cc,r]
>> Timing@andrzej[cc,r]
>> Timing@treat[cc,r]
>
>
> The function you called "andrzej" was not my function but Mark's. My
> function is below (on 1 gigabyte PowerBook)
>
>
> mukasa[cashFlow_, rate_] :=
>    Module[{nvec = Range[Length[cashFlow]]},
>     Total[cashFlow/(1. + rate)^nvec]]
> andrzej = Compile[{{cashFlow, _Real, 1}, {rate, _Real}},
>      Module[{nvec = Range[Length[cashFlow]]},
>       Total[cashFlow*(-rate^19 + rate^18 - rate^17 +
>           rate^16 - rate^15 + rate^14 - rate^13 +
>           rate^12 - rate^11 + rate^10 - rate^9 + rate^8 -
>           rate^7 + rate^6 - rate^5 + rate^4 - rate^3 +
>           rate^2 - rate + 1)^nvec]]];
> treat[cc_List, r_] := Block[{z = 1/(1 + r)},
>     z*Fold[#1*z + #2 & , 0, Reverse[cc]]]
> cashFlow[n_] := Table[Random[], {n}];
> cc = cashFlow[100000];
> r = 0.1;
> Timing[mukasa[cc, r]]
> Timing[andrzej[cc, r]]
> Timing[treat[cc, r]]
>
> Out[20]=
> {3.75*Second, 4.685760300}
>
> Out[21]=
> {0.060000000000002274*Second, 4.685760300}
>
> Out[22]=
> {0.07000000000000028*Second, 4.685760300}
>
>
>



-- 
DrBob at bigfoot.com
www.eclecticdreams.net


  • Prev by Date: RE: Re: 3D graphs with constraints
  • Next by Date: Re: Combining 2D graphs into a 3D graph
  • Previous by thread: Re: Re: Question on Compile[]
  • Next by thread: Re: Re: Question on Compile[]