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

Conversation

@jodastephen
Copy link
Member

commented Jul 11, 2019

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

Enhance performance by avoiding Math.pow
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;

This comment has been minimized.

Copy link
@wjnicholson

wjnicholson Jul 12, 2019

Member

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;

This comment has been minimized.

Copy link
@wjnicholson

wjnicholson Jul 12, 2019

Member

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?

Show resolved Hide resolved ...ta/pricer/impl/volatility/smile/SabrHaganVolatilityFunctionProvider.java

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);

This comment has been minimized.

Copy link
@wjnicholson

wjnicholson Jul 12, 2019

Member

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

This comment has been minimized.

Copy link
@wjnicholson

wjnicholson Jul 12, 2019

Member

Also nu * nu

Show resolved Hide resolved ...ta/pricer/impl/volatility/smile/SabrHaganVolatilityFunctionProvider.java
@jodastephen

This comment has been minimized.

Copy link
Member Author

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.

@wjnicholson
Copy link
Member

left a comment

Seems good to me

@jodastephen jodastephen merged commit 6e9ca6a into master Jul 15, 2019

13 checks passed

security/snyk - examples/pom.xml (OpenGamma) No new issues
Details
security/snyk - modules/calc/pom.xml (OpenGamma) No new issues
Details
security/snyk - modules/collect/pom.xml (OpenGamma) No new issues
Details
security/snyk - modules/data/pom.xml (OpenGamma) No new issues
Details
security/snyk - modules/loader/pom.xml (OpenGamma) No new issues
Details
security/snyk - modules/market/pom.xml (OpenGamma) No new issues
Details
security/snyk - modules/math/pom.xml (OpenGamma) No new issues
Details
security/snyk - modules/measure/pom.xml (OpenGamma) No new issues
Details
security/snyk - modules/pom.xml (OpenGamma) No new issues
Details
security/snyk - modules/product/pom.xml (OpenGamma) No new issues
Details
security/snyk - modules/report/pom.xml (OpenGamma) No new issues
Details
security/snyk - pom.xml (OpenGamma) No new issues
Details
workflow Workflow: workflow
Details

@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
3 participants
You can’t perform that action at this time.