3 ms·
The cached range is -128 to 127. I just reran these benchmarks with ints instead of longs and the mask changed to 127(0x7F) and sumNulls and sumSimple results a
by jnellis 4y ago
The cached range is -128 to 127. I just reran these benchmarks with ints instead of longs and the mask changed to 127(0x7F) and sumNulls and sumSimple results are exactly the same: 0.6 ns/op. As for the sumOptional method, changing Optional<Integer> for the primitive variant OptionalInt, doesn't change the result much, its the actual creation of the Optional object itself that dominates the time.
In nutshell, on my old i5 2500k:
ints 0.6ns/op
cached Integer 0.6ns/op
boxed Integer 1.3ns/op
OptionalInt 3.5ns/op
Optional<Integer> 4.2ns/op (time includes boxing the int)
Where an op is getting the number, checking it, then an addition.
For hot loops inside a jmh benchmark, you can use @OperationsPerInvocation(MAX) and it will spit out the results in this more readable format for the time just inside the loop.
- Groxx 4y agoBenchmark structure is one thing I hope more languages copy from Go - hard-coding iterations is pretty silly, doomed to need repeated changes or become problematically imprecise as hardware and runtime changes occur.