Re: Re: Question on Compile[]

• To: mathgroup at smc.vnet.net
• Subject: [mg49797] Re: [mg49786] Re: [mg49747] Question on Compile[]
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Sun, 1 Aug 2004 04:10:00 -0400 (EDT)
• References: <200407301002.GAA26336@smc.vnet.net> <200407310714.DAA12055@smc.vnet.net> <opsb0onfdbiz9bcq@monster.cox-internet.com>
• Sender: owner-wri-mathgroup at wolfram.com

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

```

• Prev by Date: The Histogram[] command
• Next by Date: infinity expression from matrix inverse
• Previous by thread: Re: The Histogram[] command
• Next by thread: Re: Re: Question on Compile[]