算法复杂度分析的确是一样,但是因为具体应用的实现不同可能多少会有点差异。coding and thinking就知道结果了 package com.wmmad.test;import junit.framework.TestCase;public class LoopTestCase extends TestCase { public void testAdd() { long result = 0; for(int j=0; j < 100; j++) { long start = System.nanoTime(); for(int i = 1; i < 100001; i++) {} long end = System.nanoTime(); result += end - start; }
System.out.println("add use time is: " + result/100.00 + "ns"); }
public void testMinus() { long result = 0; for(int j=0; j < 100; j++) { long start = System.nanoTime(); for(int i = 100000; i > 0; i--) {} long end = System.nanoTime(); result += end - start; }
System.out.println("minus use time is: " + result/100.00 + "ns"); } }
for (int i = 0; i < 10000; i++)
package com.wmmad.test;import junit.framework.TestCase;public class LoopTestCase extends TestCase {
public void testAdd() {
long result = 0;
for(int j=0; j < 100; j++) {
long start = System.nanoTime();
for(int i = 1; i < 100001; i++) {}
long end = System.nanoTime();
result += end - start;
}
System.out.println("add use time is: " + result/100.00 + "ns");
}
public void testMinus() {
long result = 0;
for(int j=0; j < 100; j++) {
long start = System.nanoTime();
for(int i = 100000; i > 0; i--) {}
long end = System.nanoTime();
result += end - start;
}
System.out.println("minus use time is: " + result/100.00 + "ns");
}
}