# 在Java中乘法和除法比移位更快吗？ 。净？

### 13 Solutions collect form web for “在Java中乘法和除法比移位更快吗？ 。净？”

• 读取汇编代码

99.9％的时候，他们试图第二次猜测现代（和所有未来）的JIT在同一时间。
99.999％，当他们试图第二次猜测现代（和所有未来）的CPU优化。

` `int a = -7; System.out.println("Shift: "+(a >> 1)); System.out.println("Div: "+(a / 2));` `

` `Shift: -4 Div: -3` `

` `r = (((top.R - bottom.R) * alpha + (bottom.R * 255)) * 0x8081) >> 23;` `

` `r = ((top.R - bottom.R) * alpha + (bottom.R * 255)) / 255;` `

（编辑：改变了代码停止溢出迈克尔·迈尔斯的build议后，但结果是一样的！这里有什么错？）

` `import java.util.Date; public class Test { public static void main(String[] args) { Date before = new Date(); for (int j = 1; j < 50000000; j++) { int a = 1 ; for (int i = 0; i< 10; i++){ a *=2; } } Date after = new Date(); System.out.println("Multiplying " + (after.getTime()-before.getTime()) + " milliseconds"); before = new Date(); for (int j = 1; j < 50000000; j++) { int a = 1 ; for (int i = 0; i< 10; i++){ a = a << 1; } } after = new Date(); System.out.println("Shifting " + (after.getTime()-before.getTime()) + " milliseconds"); } }` `

` `for (int i = 0, j = 1, k = 1; i < 10; i++) { j = j * 2; k <<= 2; Console.WriteLine("{0} {1}", j, k); }` `

` ` public static void Main(String[] args) { for (int i = 0, j = 1, k = 1; i < 10; i++) { j = j * 2; k <<= 2; Console.WriteLine("{0} {1}", j, k); } Console.WriteLine("-----------------------------------------------"); DateTime before = DateTime.Now; for (int j = 1; j < 500000000; j++) { int a = 1; for (int i = 0; i < 10; i++) a *= 2; } DateTime after = DateTime.Now; Console.WriteLine("Multiplying " + (after - before).ToString() + " milliseconds"); before = DateTime.Now; for (int j = 1; j < 500000000; j++) { int a = 1; for (int i = 0; i < 10; i++) a = a << 1; } after = DateTime.Now; Console.WriteLine("Shifting " + (after - before).ToString() + " milliseconds"); Console.ReadKey(); }` `