首页 > 其他 > 详细

倒计时后显示程序运行时间

时间:2019-06-17 00:54:02      阅读:167      评论:0      收藏:0      [点我收藏+]

 

/* 倒计时后显示程序运行时间 */

#include <time.h>
#include <stdio.h>

/*--- 等待x毫秒 ---*/
int sleep(unsigned long x)
{
    clock_t c1 = clock(), c2;

    do {
        if ((c2 = clock()) == (clock_t)-1)    /* 错误 */
            return 0;
    } while (1000.0 * (c2 - c1) / CLOCKS_PER_SEC < x); 
    return 1;
}

int main(void)
{
    int     i;
    clock_t    c;

    for (i = 10; i > 0; i--) {        /* 倒数 */
        printf("\r%2d", i);
        fflush(stdout);
        sleep(1000);                /* 暂停1秒 */
    }
    printf("\r\aFIRE!!\n");

    c = clock();
    printf("程序开始运行后经过了%.1f秒。\n",(double)c / CLOCKS_PER_SEC);
    return 0;
}

输出

FIRE!!
程序开始运行后经过了10.0秒。

 

倒计时后显示程序运行时间

原文:https://www.cnblogs.com/sea-stream/p/11037558.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!