Enhance performance by avoiding Math.pow#2027
Merged
jodastephen merged 2 commits intomasterfrom Jul 15, 2019
Merged
Conversation
Math.pow is slow when the power is a small integer This particularly affects SABR volatility adjoint
wjnicholson
reviewed
Jul 12, 2019
| } | ||
| } 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; |
Contributor
There was a problem hiding this comment.
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; |
Contributor
There was a problem hiding this comment.
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?
...a/com/opengamma/strata/pricer/impl/volatility/smile/SabrHaganVolatilityFunctionProvider.java
Show resolved
Hide resolved
wjnicholson
reviewed
Jul 12, 2019
|
|
||
| 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); |
Contributor
There was a problem hiding this comment.
(2 - 3 * rho * rho) gets reused lower down
...a/com/opengamma/strata/pricer/impl/volatility/smile/SabrHaganVolatilityFunctionProvider.java
Show resolved
Hide resolved
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. |
yukiiwashita
approved these changes
Jul 12, 2019
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Math.pow is slow when the power is a small integer
This particularly affects SABR volatility adjoint