吹毛求疵的计较下变量的位置

long i = 0;
  long j = 0;
  long flag3 = System.currentTimeMillis();
  i = 4l;
  j = 6l;
  for (long j2 = 0; j2 < 200000001L; j2++) {
   long temp = i;
   i = j;
   j = temp;
  }
  System.out.println(i + " " + j);
  long flag4 = System.currentTimeMillis();
  
  i = 4l;
  j = 6l;
  long temp = 0;
  for (long j2 = 0; j2 < 200000001L; j2++) {
   temp = i;
   i = j;
   j = temp;
  }
  System.out.println(i + " " + j);
  long flag5 = System.currentTimeMillis();

 经过比较,放在For循环外面的赋值更有效率.

 两个变量交换的扩展思考:http://blog.csdn.net/do2jiang/archive/2009/09/13/4549679.aspx

 得出一个结论,在编译器魔术影响下,我们用了^等方式来优化代码.结果反而不能被编译器优化...性能更低.

 

你可能感兴趣的:(.net,Blog,J#)