# Why is my C++ implementation of a binomial tree so slow??

#### Matt A

Hello! I'm implementing a simple binomial pricing model in C++. Right now it looks like my vectorized python approach is actually faster than my C++ implementation, which surprises me (maybe it shouldn't?). I'm wondering if there's something that can be optimized in my implementation. The bulk of the computation is done in the following function:

C++:
double iterate_tree(std::vector<double>& v_curr,
double r, double dt, double p, double q)
{
unsigned long long int N{v_curr.size()};
unsigned long long int j{0};
while(j < N)
{
for (std::vector<double>::size_type i = 0; i < N - j - 1; i++)
{
v_curr = exp(-r*dt)*(p*v_curr + q*v_curr[i+1]); // Calculate the value at each node
}
j++;
}
return v_curr;
}

The input "v_curr" is a vector of boundary conditions (ex: (ST - K)+). I'm passing the vector by reference, and modifying it in place. I don't see where the huge bottleneck comes from in terms of the computing time. I'm hoping someone can help point it out for me. Thanks!

Last edited:
Run in Release mode.

// Python faster than C++? No way.

Last edited:
MOMDELING DERIVATIVES C++
Justin London

This book might help, it has the code you are implementing

• Quasar Chunawala
MOMDELING DERIVATIVES C++
Justin London

This book might help, it has the code you are implementing

Last edited:
Even better: buy Luigi Ballabio's book

and install Quantlib C++ library.

Piece of cake.

Or my book, chapters 11 + 12

Right now it looks like my vectorized python approach is actually faster than my C++ implementation, which surprises me (maybe it shouldn't?).
Can you quantify?

• Quasar Chunawala

Replies
8
Views
1K
Replies
4
Views
2K
Replies
2
Views
1K
Replies
1
Views
2K
Replies
5
Views
2K