Re: RFR: 8285519: Change usages of TimeUnit.convert to TimeUnit.toXXX [v2]
On Sun, 15 May 2022 10:31:20 GMT, Andrey Turbanov wrote: >> TimeUnit.toMillis/toNanos/toMicros/toSeconds is shorter and a bit faster. >> Compared via JMH benchmark: 150ns -> 125ns/op >> >> Benchamark adapted from >> http://cr.openjdk.java.net/~shade/8152083/TimeUnitBench.java >> >> >> @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) >> @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) >> @Fork(1) >> @State(Scope.Thread) >> @BenchmarkMode(Mode.AverageTime) >> @OutputTimeUnit(TimeUnit.NANOSECONDS) >> public class TimeUnitBench { >> >> static final int SIZE = TimeUnit.values().length * 10; >> >> @Param({"0", "1", "2", "3", "4", "5", "6", "-1"}) >> int bias; >> >> TimeUnit[] mod; >> >> @Setup >> public void setup() { >> mod = new TimeUnit[SIZE]; >> >> TimeUnit[] vals = TimeUnit.values(); >> for (int c = 0; c < SIZE; c++) { >> if (bias == -1) { >> // megamorphic >> mod[c] = vals[c % vals.length]; >> } else { >> mod[c] = vals[bias]; >> } >> } >> >> Random r = new Random(); >> for (int c = 0; c < 1; c++) { >> int i = r.nextInt(); >> for (int o = 0; o < vals.length; o++) { >> if (vals[o].toNanos(i) != TimeUnit.NANOSECONDS.convert(i, >> vals[o])) >> throw new IllegalStateException("switch " + o); >> } >> } >> } >> >> @Benchmark >> public void const_convert(Blackhole bh) { >> for (TimeUnit tu : mod) { >> bh.consume(do_convert(tu)); >> } >> } >> >> @Benchmark >> public void value_convert(Blackhole bh) { >> for (TimeUnit tu : mod) { >> bh.consume(do_convert(tu, 1L)); >> } >> } >> >> @Benchmark >> public void const_toNanos(Blackhole bh) { >> for (TimeUnit tu : mod) { >> bh.consume(do_toNanos(tu)); >> } >> } >> >> @Benchmark >> public void value_toNanos(Blackhole bh) { >> for (TimeUnit tu : mod) { >> bh.consume(do_toNanos(tu, 1L)); >> } >> } >> >> @CompilerControl(CompilerControl.Mode.DONT_INLINE) >> private long do_toNanos(TimeUnit tu) { >> return tu.toNanos(1L); >> } >> >> @CompilerControl(CompilerControl.Mode.DONT_INLINE) >> private long do_toNanos(TimeUnit tu, long value) { >> return tu.toNanos(value); >> } >> >> @CompilerControl(CompilerControl.Mode.DONT_INLINE) >> private long do_convert(TimeUnit tu) { >> return TimeUnit.NANOSECONDS.convert(1L, tu); >> } >> >> @CompilerControl(CompilerControl.Mode.DONT_INLINE) >> private long do_convert(TimeUnit tu, long value) { >> return TimeUnit.NANOSECONDS.convert(value, tu); >> } >> } >> >> Results: >> >> Benchmark(bias) Mode CntScoreError Units >> TimeUnitBench.const_convert 0 avgt5 151,856 ± 28,595 ns/op >> TimeUnitBench.const_convert 1 avgt5 150,720 ± 23,863 ns/op >> TimeUnitBench.const_convert 2 avgt5 151,432 ± 23,184 ns/op >> TimeUnitBench.const_convert 3 avgt5 150,959 ± 24,726 ns/op >> TimeUnitBench.const_convert 4 avgt5 150,966 ± 25,280 ns/op >> TimeUnitBench.const_convert 5 avgt5 150,976 ± 25,542 ns/op >> TimeUnitBench.const_convert 6 avgt5 151,118 ± 25,254 ns/op >> TimeUnitBench.const_convert -1 avgt5 152,673 ± 29,861 ns/op >> TimeUnitBench.const_toNanos 0 avgt5 121,296 ± 25,236 ns/op >> TimeUnitBench.const_toNanos 1 avgt5 121,707 ± 25,364 ns/op >> TimeUnitBench.const_toNanos 2 avgt5 121,736 ± 25,726 ns/op >> TimeUnitBench.const_toNanos 3 avgt5 121,822 ± 25,491 ns/op >> TimeUnitBench.const_toNanos 4 avgt5 121,867 ± 26,090 ns/op >> TimeUnitBench.const_toNanos 5 avgt5 121,927 ± 25,611 ns/op >> TimeUnitBench.const_toNanos 6 avgt5 121,821 ± 25,843 ns/op >> TimeUnitBench.const_toNanos -1 avgt5 121,923 ± 25,206 ns/op >> TimeUnitBench.value_convert 0 avgt5 150,525 ± 25,439 ns/op >> TimeUnitBench.value_convert 1 avgt5 151,098 ± 24,024 ns/op >> TimeUnitBench.value_convert 2 avgt5 151,259 ± 25,381 ns/op >> TimeUnitBench.value_convert 3 avgt5 151,030 ± 25,548 ns/op >> TimeUnitBench.value_convert 4 avgt5 151,290 ± 25,998 ns/op >> TimeUnitBench.value_convert 5 avgt5 151,006 ± 25,448 ns/op >> TimeUnitBench.value_convert 6 avgt5 150,945 ± 25,314 ns/op >> TimeUnitBench.value_convert -1 avgt5 151,186 ± 25,814 ns/op >> TimeUnitBench.value_toNanos 0 avgt5 121,556 ± 25,745 ns/op >> TimeUnitBench.value_toNanos 1 avgt5 123,410 ± 22,323 ns/op >> TimeUnitBench.value_toNanos 2 avgt5 125,452
Re: RFR: 8285519: Change usages of TimeUnit.convert to TimeUnit.toXXX [v2]
> TimeUnit.toMillis/toNanos/toMicros/toSeconds is shorter and a bit faster. > Compared via JMH benchmark: 150ns -> 125ns/op > > Benchamark adapted from > http://cr.openjdk.java.net/~shade/8152083/TimeUnitBench.java > > > @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) > @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) > @Fork(1) > @State(Scope.Thread) > @BenchmarkMode(Mode.AverageTime) > @OutputTimeUnit(TimeUnit.NANOSECONDS) > public class TimeUnitBench { > > static final int SIZE = TimeUnit.values().length * 10; > > @Param({"0", "1", "2", "3", "4", "5", "6", "-1"}) > int bias; > > TimeUnit[] mod; > > @Setup > public void setup() { > mod = new TimeUnit[SIZE]; > > TimeUnit[] vals = TimeUnit.values(); > for (int c = 0; c < SIZE; c++) { > if (bias == -1) { > // megamorphic > mod[c] = vals[c % vals.length]; > } else { > mod[c] = vals[bias]; > } > } > > Random r = new Random(); > for (int c = 0; c < 1; c++) { > int i = r.nextInt(); > for (int o = 0; o < vals.length; o++) { > if (vals[o].toNanos(i) != TimeUnit.NANOSECONDS.convert(i, > vals[o])) > throw new IllegalStateException("switch " + o); > } > } > } > > @Benchmark > public void const_convert(Blackhole bh) { > for (TimeUnit tu : mod) { > bh.consume(do_convert(tu)); > } > } > > @Benchmark > public void value_convert(Blackhole bh) { > for (TimeUnit tu : mod) { > bh.consume(do_convert(tu, 1L)); > } > } > > @Benchmark > public void const_toNanos(Blackhole bh) { > for (TimeUnit tu : mod) { > bh.consume(do_toNanos(tu)); > } > } > > @Benchmark > public void value_toNanos(Blackhole bh) { > for (TimeUnit tu : mod) { > bh.consume(do_toNanos(tu, 1L)); > } > } > > @CompilerControl(CompilerControl.Mode.DONT_INLINE) > private long do_toNanos(TimeUnit tu) { > return tu.toNanos(1L); > } > > @CompilerControl(CompilerControl.Mode.DONT_INLINE) > private long do_toNanos(TimeUnit tu, long value) { > return tu.toNanos(value); > } > > @CompilerControl(CompilerControl.Mode.DONT_INLINE) > private long do_convert(TimeUnit tu) { > return TimeUnit.NANOSECONDS.convert(1L, tu); > } > > @CompilerControl(CompilerControl.Mode.DONT_INLINE) > private long do_convert(TimeUnit tu, long value) { > return TimeUnit.NANOSECONDS.convert(value, tu); > } > } > > Results: > > Benchmark(bias) Mode CntScoreError Units > TimeUnitBench.const_convert 0 avgt5 151,856 ± 28,595 ns/op > TimeUnitBench.const_convert 1 avgt5 150,720 ± 23,863 ns/op > TimeUnitBench.const_convert 2 avgt5 151,432 ± 23,184 ns/op > TimeUnitBench.const_convert 3 avgt5 150,959 ± 24,726 ns/op > TimeUnitBench.const_convert 4 avgt5 150,966 ± 25,280 ns/op > TimeUnitBench.const_convert 5 avgt5 150,976 ± 25,542 ns/op > TimeUnitBench.const_convert 6 avgt5 151,118 ± 25,254 ns/op > TimeUnitBench.const_convert -1 avgt5 152,673 ± 29,861 ns/op > TimeUnitBench.const_toNanos 0 avgt5 121,296 ± 25,236 ns/op > TimeUnitBench.const_toNanos 1 avgt5 121,707 ± 25,364 ns/op > TimeUnitBench.const_toNanos 2 avgt5 121,736 ± 25,726 ns/op > TimeUnitBench.const_toNanos 3 avgt5 121,822 ± 25,491 ns/op > TimeUnitBench.const_toNanos 4 avgt5 121,867 ± 26,090 ns/op > TimeUnitBench.const_toNanos 5 avgt5 121,927 ± 25,611 ns/op > TimeUnitBench.const_toNanos 6 avgt5 121,821 ± 25,843 ns/op > TimeUnitBench.const_toNanos -1 avgt5 121,923 ± 25,206 ns/op > TimeUnitBench.value_convert 0 avgt5 150,525 ± 25,439 ns/op > TimeUnitBench.value_convert 1 avgt5 151,098 ± 24,024 ns/op > TimeUnitBench.value_convert 2 avgt5 151,259 ± 25,381 ns/op > TimeUnitBench.value_convert 3 avgt5 151,030 ± 25,548 ns/op > TimeUnitBench.value_convert 4 avgt5 151,290 ± 25,998 ns/op > TimeUnitBench.value_convert 5 avgt5 151,006 ± 25,448 ns/op > TimeUnitBench.value_convert 6 avgt5 150,945 ± 25,314 ns/op > TimeUnitBench.value_convert -1 avgt5 151,186 ± 25,814 ns/op > TimeUnitBench.value_toNanos 0 avgt5 121,556 ± 25,745 ns/op > TimeUnitBench.value_toNanos 1 avgt5 123,410 ± 22,323 ns/op > TimeUnitBench.value_toNanos 2 avgt5 125,452 ± 26,575 ns/op > TimeUnitBench.value_toNanos 3 avgt5 125,297 ± 26,204 ns/op > TimeUnitBench.value_toNanos 4 avgt5 125,357 ± 26,085 ns/op >