(original) (raw)
/* tested with: OpenJDK Runtime Environment 18.9 (build 11-ea+13) OpenJDK 64-Bit Server VM 18.9 (build 11-ea+13, mixed mode) ~/java11/jdk/bin/java -jar target/benchmarks.jar -i 5 -wi 5 -f 2 -tu ns Benchmark (arg) Mode Samples Score Score error Units o.s.MyBenchmark.testBase 1 avgt 10 1.098 0.040 ns/op o.s.MyBenchmark.testBase 42 avgt 10 1.088 0.060 ns/op o.s.MyBenchmark.testNew 1 avgt 10 2.499 0.072 ns/op o.s.MyBenchmark.testNew 42 avgt 10 2.570 0.129 ns/op o.s.MyBenchmark.testOld 1 avgt 10 4.344 0.215 ns/op o.s.MyBenchmark.testOld 42 avgt 10 4.349 0.201 ns/op */ /* * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package org.openjdk; import org.openjdk.jmh.annotations.*; @BenchmarkMode(Mode.AverageTime) @State(Scope.Benchmark) public class TableFor { @Param({"1", "42"}) public int arg; @Benchmark public int testBase() { return (arg + 1) + (arg + 42); } @Benchmark public int numberOfLeadingZeros() { return Integer.numberOfLeadingZeros(arg) + Integer.numberOfLeadingZeros(arg + 42); } @Benchmark public int numberOfLeadingZeros2() { return numberOfLeadingZeros2(arg) + numberOfLeadingZeros2(arg + 42); } @Benchmark public int testOld() { return tableSizeFor(arg + 1) + tableSizeFor(arg + 42); } @Benchmark public int testNew() { return tableSizeFor_new(arg + 1) + tableSizeFor_new(arg + 42); } @Benchmark public int testNew2() { return tableSizeFor_new2(arg + 1) + tableSizeFor_new2(arg + 42); } static final int MAXIMUM_CAPACITY = 1 << 30; static final int tableSizeFor(int cap) { int n = cap - 1; n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; } static final int numberOfLeadingZeros2(int i) { // HD, Figure 5-6 if (i <= 0) return i == 0 ? 32 : 0; int n = 0; if ( i < 1 << 16) { n += 16; i <<= 16; } if (i >= 0 && i < 1 << 24) { n += 8; i <<= 8; } if (i >= 0 && i < 1 << 28) { n += 4; i <<= 4; } if (i >= 0 && i < 1 << 30) { n += 2; i <<= 2; } if (i >= 0) n++; return n; } static final int tableSizeFor_new(int cap) { int n = -1 >>> Integer.numberOfLeadingZeros(cap - 1); return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; } static final int tableSizeFor_new2(int cap) { int n = -1 >>> numberOfLeadingZeros2(cap - 1); return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; } }