ax1nch commented on PR #1628:
URL: https://github.com/apache/commons-lang/pull/1628#issuecomment-4322821151
@garydgregory
Sure.. Earlier I had tried to measure in isolation for when the
short-circuiting code applies. I guess below is what you are looking at. The
result summary and the corresponding code comparing old code vs new code:
```
.
.
Result "org.ex.NumericFloatBenchmark.testDefaultCheck":
86.264 ±(99.9%) 0.854 ns/op [Average]
(min, avg, max) = (84.942, 86.264, 87.723), stdev = 0.799
CI (99.9%): [85.409, 87.118] (assumes normal distribution)
.
.
Result "org.ex.NumericFloatBenchmark.testShortcircuitCheck":
1.182 ±(99.9%) 0.053 ns/op [Average]
(min, avg, max) = (1.106, 1.182, 1.276), stdev = 0.050
CI (99.9%): [1.129, 1.235] (assumes normal distribution)
.
.
.
Benchmark Mode Cnt Score Error Units
NumericFloatBenchmark.testDefaultCheck avgt 15 86.264 ± 0.854 ns/op
NumericFloatBenchmark.testShortcircuitCheck avgt 15 1.182 ± 0.053 ns/op
```
Pls note that I've copied over couple of private methods from `NumberUtils`
to ensure the check remains verbatim
```
import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
@Fork(3)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
public class NumericFloatBenchmark {
private final String str = "0.25";
private final String mant="0";
private final String dec="25";
Float f = Float.valueOf(str);
Double d = Double.valueOf(str);
@Benchmark
public boolean testDefaultCheck() {
return !f.isInfinite() && !(f.floatValue() == 0.0F && !isZero(mant,
dec)) && f.toString().equals(d.toString());
}
@Benchmark
public boolean testShortcircuitCheck() {
return !f.isInfinite() && !(f.floatValue() == 0.0F && !isZero(mant,
dec))
&& ((double) d.floatValue() == d.doubleValue() ||
f.toString().equals(d.toString()));
}
private static boolean isZero(final String mant, final String dec) {
return isAllZeros(mant) && isAllZeros(dec);
}
private static boolean isAllZeros(final String str) {
if (str == null) {
return true;
}
for (int i = str.length() - 1; i >= 0; i--) {
if (str.charAt(i) != '0') {
return false;
}
}
return true;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]