public class DuffDevice {
private static int duff2(int count) {
int i = 0;
do {
++i;
} while (--count >0);
return i;
}
public static void main(String[] args) {
int duff;
long start, end;
DuffDevice duffDevice = new DuffDevice();
start = System.currentTimeMillis();
duff = duff2(Integer.MAX_VALUE - 7);
end = System.currentTimeMillis();
System.out.println(duff + " " + (end - start) + " ms");
}
}
C
#include
#include
int duff(int count) {
}
int main(int argc, char* argv[]) {
struct timeval start_time, end_time;
gettimeofday(&start_time, NULL);
long count = 2147483640;
long i = 0;
do {
++i;
} while (--count);
gettimeofday(&end_time, NULL);
int total_time = 1000000 * (end_time.tv_sec - start_time.tv_sec) + (end_time.tv_usec - start_time.tv_usec);
printf("%d us", total_time);
printf("\n");
printf("%.3f ms", (double) total_time / 1000);
printf("\n");
printf("%l", i);
return 0;
}
Java 耗时情况
2 ms
C 耗时情况
4638.628 ms
性能怎么会相差这么大?