Long
begin
=System.nanoTime();
C = A;
A = B;
B = C;
System.out.println("A=" + A);
System.out.println("B=" + B);
Long
end
=System.nanoTime();
System.out.println("Start Time: "+begin);
System.out.println("End Time: "+end);
自考Java语言快速入门Java 语言基础Java 语言概述 System.out.println("Last Time: "+(end-begin));
System.out.println("==================");
System.out.println("Method 2 starting…");
begin=System.nanoTime();
A = A + B;
B = A
–
B;
A = A
–
B;
System.out.println("A=" + A);
System.out.println("B=" + B);
end=System.nanoTime();
System.out.println("Start Time: "+begin);
System.out.println("End Time: "+end);
System.out.println("Last Time: "+(end-begin));
System.out.println("==================");
System.out.println("Method 3 starting…");
begin=System.nanoTime();
A = A ^ B;
B = A ^ B;
A = A ^ B;
System.out.println("A=" + A);
System.out.println("B=" + B);
end=System.nanoTime();
System.out.println("Start Time: "+begin);
System.out.println("End Time: "+end);
System.out.println("Last Time: "+(end-begin));
System.out.println("==================");
}
}
运行结果:
Object creating…
A=2
B=3
==================
Method 1 starting…
A=3
B=2
Start Time: 7022865296008
End Time: 7022865590179
Last Time: 294171
==================
Method 2 starting…
A=2
B=3
Start Time: 7022866909621
End Time: 7022867035335
Last Time: 125714
==================
Method 3 starting…
A=3
B=2
Start Time: 7022867374764
End Time: 7022867490700
Last Time: 115936