Skip to main content

Searching QuantLib::Dates, Part 3

Updated 1 min readMarkdown
Part of seriesSearching QuantLib::Dates

Comparing QuantLib::Date objects by their serial number has given us great performance gains, but we can do better.

We could continue to push the data points into our output collection individually, but this is inefficient:

  • At some point, our std::vector will have to grow to accommodate another data point. This could be mitigated by calling reserve() on the vector, but that could be inefficient for memory use.

  • Checking the data points one-by-one means branching, which may lead to inefficiencies.

It would be preferable to copy all of the required data into our output collection in one go. The key observation is that the QuantLib dates are in a std::map, which is an ordered collection. This allows us to find the range containing the data points we're interested in.

The initial step is to find the iterators that bound the data set of interest:

auto startPosition = indexLevels.lower_bound(startOfSearchMonth);
auto endPosition = indexLevels.upper_bound(endOfSearchMonth);

With these iterators in hand, we can copy the data points into our output collection in one go!

const IndexData discoveredPoints(startPosition, endPosition);

This results in a search that is 10x faster than the previous iteration of our function:

About the author

  • Developer based in London. Mainly programming in C++ now, but with some skills in Python and C#. About 19 years experience working on Financial Software - may look to branch out at some point.

No comments yet. Be the first to comment.