⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p7-6.c

📁 UNIX程序设计教程
💻 C
字号:
#include <stdio.h>#include <sys/types.h>#include <sys/times.h>#include <unistd.h>#include <sys/time.h>void work();int main (void){    struct timeval start, stop,diff;    double total_sec;    clock_t b,e,t;    struct tms buf;    gettimeofday(&start,NULL);      work(5);     /* 程序7-2中的work函数 */     gettimeofday(&stop,NULL);    timeval_subtract(&diff,&stop, &start);    total_sec = (double)(diff.tv_sec*1000000+diff.tv_usec)                /(double)1000000;    printf("tatol wall time : %lf seconds\n",total_sec);    return 0;}/* 类型为'struct timeval'的两个对象x和y相减,存储结果于result。   若差为负数,返回1,否则返回0.*/int timeval_subtract(struct timeval *result,                      struct timeval *x, struct timeval *y){    if (x->tv_usec < y->tv_usec) {        int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;        y->tv_usec -= 1000000 * nsec;        y->tv_sec += nsec;    }    if (x->tv_usec - y->tv_usec > 1000000) {       int nsec = (y->tv_usec - x->tv_usec) / 1000000;       y->tv_usec += 1000000 * nsec;       y->tv_sec -= nsec;    }    result->tv_sec = x->tv_sec - y->tv_sec;    result->tv_usec = x->tv_usec - y->tv_usec;    /* 若结果为负则返回1 */return x->tv_sec < y->tv_sec;}void work(int k)       /* 模拟工作 */{    FILE *f;    int i;    double x=4.5;    f = tmpfile();    for(i = 0; i < k*100000; i++){        fprintf(f,"Do some output\n");        if(ferror(f)){            fprintf(stderr,"Error writing to temorary file\n");            exit(1);        }    }    for(i=0;i<k*100000;i++)        x=log(x*x+3.21);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -