Skip to content

Enhance performance by avoiding Math.pow#2027

Merged
jodastephen merged 2 commits intomasterfrom
topic/sabr-performance
Jul 15, 2019
Merged

Enhance performance by avoiding Math.pow#2027
jodastephen merged 2 commits intomasterfrom
topic/sabr-performance

Conversation

@jodastephen
Copy link
Contributor

Math.pow is slow when the power is a small integer
This particularly affects SABR volatility adjoint

Math.pow is slow when the power is a small integer
This particularly affects SABR volatility adjoint
}
} else {
rhoBar = (1 / (Math.sqrt(1 - 2 * rho * z + z * z) + z - rho) *
(-Math.pow(1 - 2 * rho * z + z * z, -0.5) * z - 1) + 1 / rhoStar) * xzBar;
Copy link
Contributor

@wjnicholson wjnicholson Jul 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line also reuses 1 - 2 * rho * z + z * z

* @return {@code value * value * value * value}
*/
public static double pow4(double value) {
return value * value * value * value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're doing micro optimisation, is there any performance difference between this and doing

double sq = value * value;
return sq * sq;

Or is that equivalent to what's done in hotspot?


double sf1 = sfK * (1 + (betaStarPow2 / 24) * lnrfKPow2 + betaStarPow4Div1920 * lnrfKPow4);
double sf2 = (1 + (pow2(betaStar * alpha / sfK) / 24 + (rho * beta * nu * alpha) /
sfKMul4 + (2 - 3 * rho * rho) * nu * nu / 24) * timeToExpiry);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(2 - 3 * rho * rho) gets reused lower down

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also nu * nu

@jodastephen
Copy link
Contributor Author

I've made some more changes. The goal isn't to fully optimise as there are too many code paths. Instead, I'm just looking for some key changes around pow/sqrt. Not every extraction is necessarily faster as too many local variables may well be a problem too.

Copy link
Contributor

@wjnicholson wjnicholson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me

@jodastephen jodastephen merged commit 6e9ca6a into master Jul 15, 2019
@delete-merged-branch delete-merged-branch bot deleted the topic/sabr-performance branch July 15, 2019 14:16
@jodastephen jodastephen added this to the v2.6 milestone Jul 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants