Question on Compile[]
- To: mathgroup at smc.vnet.net
- Subject: [mg49747] Question on Compile[]
- From: Mark Coleman <mark at markscoleman.com>
- Date: Fri, 30 Jul 2004 06:02:01 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Greetings,
I am doing some research in finance, translating some visual basic code
into Mathematica, and attempting to optimize it. Most of this is quite
straightforward, and by Compiling[] functions, execution is quite fast.
I did run into an instance where the compilied code ran a bit slower
than the uncompiled code. I am sure this has something to do with some
additional work that Mathematica is doing internally to expand the expression I
am using, but I was hoping someone on the list could help me understand
and if possible fix, this behavior.
Specifically I am calculating a standard discounted cash flow. The
computation is quite simple, Let
cashFlow be a list of real numbers of length n and let nvec=Range[n],
then the discounted cash flow is the sum over i=1,n of
cashFlow(i)/(1+r)^i
where r is a real number with 0 < r < 1.
The final Mathematica code is:
Clear[dcf];
dcf[cashFlow_, rate_] := Module[{nvec = Range[Length[cashFlow]]},
Total[cashFlow/(1.0 + rate)^nvec]
]
On my 800 Mhz Powerbook G4, 5000 trials of this takes 3.72 seconds,
while the compiled version takes 4.2 seconds.
Any ideas how I can speed this up, to get the usual gains one typically
sees in compiled code?
Thanks,
-Mark
- Follow-Ups:
- Re: Question on Compile[]
- From: Andrzej Kozlowski <akoz@mimuw.edu.pl>
- Re: Question on Compile[]
- From: Sseziwa Mukasa <mukasa@jeol.com>
- Re: Question on Compile[]