网站备案可以做电影网站吗,wordpress主题翻译插件,智游泰州小程序怎么注册,企业+网站+wordpressjava和golang性能最初的问题是如何计算二进制数中1的个数#xff1f; 我包含了使用Integer.bitCount#xff08;#xff09;的性能比较#xff0c;该性能可以转换为固有特性#xff0c;即单个机器代码指令POPCNT和执行相同功能的Java代码。 题 如何计算二进制数中1的个数… java和golang性能 最初的问题是如何计算二进制数中1的个数 我包含了使用Integer.bitCount的性能比较该性能可以转换为固有特性即单个机器代码指令POPCNT和执行相同功能的Java代码。 题 如何计算二进制数中1的个数 假设我有数字45它等于二进制数101101其中有4 1。 编写算法来执行此操作的最有效方法是什么 回答 与其编写算法来执行此操作不如使用内置函数。 Integer.bitCount 使之特别有效的原因是JVM可以将其视为内部函数。 即在支持它的平台例如Intel / AMD上用单个机器代码指令识别并替换整个事物 演示此优化的有效性 public static void main(String... args) {perfTestIntrinsic();perfTestACopy();
}private static void perfTestIntrinsic() {long start System.nanoTime();long countBits 0;for (int i 0; i Integer.MAX_VALUE; i)countBits Integer.bitCount(i);long time System.nanoTime() - start;System.out.printf(Intrinsic: Each bit count took %.1f ns, countBits%d%n, (double) time / Integer.MAX_VALUE, countBits);
}private static void perfTestACopy() {long start2 System.nanoTime();long countBits2 0;for (int i 0; i Integer.MAX_VALUE; i)countBits2 myBitCount(i);long time2 System.nanoTime() - start2;System.out.printf(Copy of same code: Each bit count took %.1f ns, countBits%d%n, (double) time2 / Integer.MAX_VALUE, countBits2);
}// Copied from Integer.bitCount()
public static int myBitCount(int i) {// HD, Figure 5-2i i - ((i 1) 0x55555555);i (i 0x33333333) ((i 2) 0x33333333);i (i (i 4)) 0x0f0f0f0f;i i (i 8);i i (i 16);return i 0x3f;
} 版画 Intrinsic: Each bit count took 0.4 ns, countBits33285996513
Copy of same code: Each bit count took 2.4 ns, countBits33285996513 使用固有版本和循环的每个位数平均仅需要0.4纳秒。 使用相同代码的副本要花费6倍的时间获得相同的结果 参考 Vanilla Java博客上来自我们JCG合作伙伴 Peter Lawrey的Java Intrinsics and Performance 。 翻译自: https://www.javacodegeeks.com/2012/11/java-intrinsics-and-performance.htmljava和golang性能