一般而言,移位操作符的处理速度快于算术运算

示例代码如下

 1 public class Test
2
3 {
4
5 public static void main(String[] args)
6
7 {
8
9 int low = 0;
10 int high = 9;
11
12
13
14 long startTime = System.currentTimeMillis();
15
16 for (int i = 0; i < Integer.MAX_VALUE; ++i)
17 {
18
19 int temp = (low + high) >>> 1;
20 }
21
22 long midTime = System.currentTimeMillis();
23
24 for (int i = 0; i < Integer.MAX_VALUE; ++i)
25 {
26
27 int temp = (low + high) / 2;
28 }
29
30 long endTime = System.currentTimeMillis();
31
32
33
34 System.out.println(midTime - startTime);
35
36 System.out.println(endTime - midTime);
37
38 }
39
40 }

输出结果如下

1 1804
2 7699

 

posted on 2011-12-21 15:36  cssin  阅读(372)  评论(0)    收藏  举报

导航