Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance performance by avoiding Math.pow #2027

Merged
merged 2 commits into from Jul 15, 2019
Merged

Conversation

@jodastephen
Copy link
Member

@jodastephen jodastephen commented Jul 11, 2019

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
double zStar = 1 - z;
double zDivZStar = z / zStar;
rhoBar = (0.5 * pow2(zDivZStar) +
0.25 * (z - 4.0) * pow3(zDivZStar) / zStar * rhoStar) * xzBar;
}
} 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
Member

@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
Member

@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.

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
Member

@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.

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

Copy link
Member

@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.

Also nu * nu

@jodastephen
Copy link
Member Author

@jodastephen jodastephen commented Jul 12, 2019

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
Member

@wjnicholson wjnicholson left a comment

Seems good to me

@jodastephen jodastephen merged commit 6e9ca6a into master Jul 15, 2019
13 checks passed
@delete-merged-branch delete-merged-branch bot deleted the topic/sabr-performance branch Jul 15, 2019
@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
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants