1: Linux计算时间间隔:
2:
3:
4: #include<time.h>
5: #include<stdio.h>
6:
7: /*
8: int gettimeofday(struct timeval *tv, struct timezone *tz);
9: int settimeofday(const struct timeval *tv , const struct timezone *tz);
10: strut timeval {
11: long tv_sec; /* 秒数 */
12: long tv_usec; /* 微秒数 */
13: };
14: */
15:
16: struct timeval start,end;
17: int timeuse;
18:
19: void time()
20: {
21: gettimeofday( &end, NULL );
22: timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
23: timeuse /= 1000000;
24: printf("time used: %ds/n", timeuse);
25: }
26:
27: void foo()
28: {
29: int i=0;
30: for(;i<10000;i++);
31: }
32:
33: int main()
34: {
35: gettimeofday( &start, NULL );
36: foo();
37: time();
38: return 0;
39: }