Login  Register

Repetitive function calls in discountingbondengine.cpp

Posted by StephenWong on Dec 22, 2011; 10:45pm
URL: http://quantlib.414.s1.nabble.com/Repetitive-function-calls-in-discountingbondengine-cpp-tp12769.html

Within the file ql/pricingengines/bond/discountingbondengine.cpp there is the DiscountingBondEngine::calculate() method. Within this method, it sets results_.value and results_.settlementValue by calling the CashFlows::npv(...) function each time. It does this even if all the arguments of the two calls are identical.

Can we not check the arguments first and if they are identical, just call the CashFlows::npv(...) once? It is a bit of an eye sore for me to see all the other functions being called within CashFlows::npv(...) over and over even if there is no such need?

More explicitly, after the line

        results_.value = CashFlows::npv(arguments_.cashflows,
                                        **discountCurve_,
                                        includeRefDateFlows,
                                        results_.valuationDate,
                                        results_.valuationDate);

just insert

if ( includeRefDateFlows == false && results_.valuationDate == arguments_.settlementDate )
     results_.settlementValue = results.value;
else {

     results_.settlementValue = CashFlows::npv(arguments_.cashflows,
                                                                **discountCurve_,
                                                                false,
                                                                arguments_.settlementDate,
                                                                arguments_.settlementDate);

}
 
I know this does not look like much but it is a bit of a pain in the neck if one is going through the code, debugging etc and see all the function calls being made over and over when there is no need.