MathGroup Archive 2004

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

Search the Archive

Question on Compile[]

  • To: mathgroup at
  • Subject: [mg49747] Question on Compile[]
  • From: Mark Coleman <mark at>
  • Date: Fri, 30 Jul 2004 06:02:01 -0400 (EDT)
  • Sender: owner-wri-mathgroup at


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


where r is a real number with 0 < r < 1.

The final Mathematica code is:

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?



  • Prev by Date: Re: pattern matching and droping elements from list of lists.
  • Next by Date: Re: pattern matching and droping elements from list of lists.
  • Previous by thread: DiscreteDelta Evaluation Question
  • Next by thread: Re: Question on Compile[]