BigInteger: Replace ==ZERO by isZero. #302
Merged
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.
Replacing
== ZERO
byisZero()
Motivation
Similar of #301 but for BigInteger. Equals are more costly than testing isZero or isNegative.
Special case
BigInteger.pow(BigInteger)
usesexponentiationBySquaring
which is now using isZero, but I was not able to reach this code path in my tests.JVM was triggering an OutOfMemory (Java heap space) after 90s of running (my exponent was
BigInteger.parse(Long.MAX_VALUE + "1")
).I took a shortcut by commenting the call to pow(Long), so that it forces the use of
exponentiationBySquaring
, and allow me to benchmark with "little" values (1 googol being "little").Benchmark (JVM)
Best of 3 runs, custom iterations (see benchmark code), JVM, M3 Pro with 36GB RAM, duration in seconds
Benchmark code
Raw data
Before
Run 1
subtract: 1.526356041s
ZERO.subtract(): 581.206625ms
pow: 1.199554083s
subtract(ZERO): 682.647833ms
modInverse: 586.281166ms
times(Char): 2.274098500s
pow: 89.229875ms
Run 2
subtract: 1.380918708s
ZERO.subtract(): 621.464125ms
pow: 1.098721042s
subtract(ZERO): 693.304667ms
modInverse: 608.127292ms
times(Char): 2.287946833s
pow: 83.628250ms
Run 3
subtract: 1.133838833s
ZERO.subtract(): 645.497916ms
pow: 1.234771250s
subtract(ZERO): 687.548750ms
modInverse: 649.144083ms
times(Char): 2.287897334s
pow: 80.517ms
After
Run 1
subtract: 1.078803625s
ZERO.subtract(): 536.864958ms
pow: 1.128199250s
subtract(ZERO): 329.929625ms
modInverse: 611.962750ms
times(Char): 2.214370791s
pow: 79.141750ms
Run 2
subtract: 1.137907458s
ZERO.subtract(): 525.679166ms
pow: 1.083095208s
subtract(ZERO): 544.093ms
modInverse: 599.358250ms
times(Char): 2.156597666s
pow: 86.663125ms
Run 3
subtract: 1.449096584s
ZERO.subtract(): 502.667083ms
pow: 1.076523292s
subtract(ZERO): 393.253875ms
modInverse: 574.755417ms
times(Char): 2.203168959s
pow: 69.521125ms