|

楼主 |
发表于 2004-8-18 08:11:22
|
显示全部楼层
- #include <stdio.h>
- #include <sys/time.h>
- const int LOOP = 1000000;
- int main(int argc, char *argv[])
- {
- int num, count, i, ms;
- struct timeval tv1, tv2;
- gettimeofday(&tv1, NULL);
- for (i=0; i<LOOP; i++) (void)count1(i);
- gettimeofday(&tv2, NULL);
- ms = (tv2.tv_sec - tv1.tv_sec) * 1000 + (tv2.tv_usec - tv1.tv_usec) / 1000;
- printf("count1 - %d ms.\n", ms);
- gettimeofday(&tv1, NULL);
- for (i=0; i<LOOP; i++) (void)count2(i);
- gettimeofday(&tv2, NULL);
- ms = (tv2.tv_sec - tv1.tv_sec) * 1000 + (tv2.tv_usec - tv1.tv_usec) / 1000;
- printf("count2 - %d ms.\n", ms);
- /* 以下省略 ............... */
- return 0;
- }
复制代码
count1(), count2() 就是我前面说的func()的不同实现
哦,测试程序是有点问题,因为都是 i 比较小的情况下得出的,楼上可以再编个程序测一测 |
|