标签:style blog class code java color
C语言的指针跳转访问是一个灵活高效的机制,但是再高效也是要花费额外时间的,下面这个程序如果在gcc -O0不优化的情况,就可以看出指针跳转比直接访问要多消耗10%左右的时间
#include <stdio.h> #include <sys/time.h> int main() { int m = 0; int *p = &m; int i =0; int n = 1; struct timeval time1, time2; m = 0; gettimeofday(&time1, 0); for(i = 0; i < 1000000000; i++) { *p = *p + n; } gettimeofday(&time2, 0); printf("time %f\n", (double)(time2.tv_sec-time1.tv_sec) + (double)(time2.tv_usec-time1.tv_usec) /1000000); m = 0; gettimeofday(&time1, 0); for(i = 0; i < 1000000000; i++) { m = m + n; } gettimeofday(&time2, 0); printf("time %f\n", (double)(time2.tv_sec-time1.tv_sec) + (double)(time2.tv_usec-time1.tv_usec) /1000000); printf("m %d\n", m); return 0; }
标签:style blog class code java color
原文地址:http://www.cnblogs.com/hdflzh/p/3715604.html