# 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[0];
}

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:

#### Daniel Duffy

##### C++ author, trainer
Run in Release mode.

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

Last edited:

#### ExSan

MOMDELING DERIVATIVES C++
Justin London

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

#### Daniel Duffy

##### C++ author, trainer
MOMDELING DERIVATIVES C++
Justin London

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

Last edited:

#### Daniel Duffy

##### C++ author, trainer
Even better: buy Luigi Ballabio's book

and install Quantlib C++ library.

Piece of cake.

#### Daniel Duffy

##### C++ author, trainer
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?

Replies
1
Views
1K
Replies
5
Views
1K
Replies
0
Views
1K
Replies
2
Views
5K
Replies
9
Views
5K