鱼C论坛

 找回密码
 立即注册
查看: 2429|回复: 5

clock()的用法

[复制链接]
发表于 2021-5-11 20:55:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请问为何本类计时代码在我的电脑运行后打印秒数总是0.0秒?

int main() {
            ....

        start = clock();
        .....程序块......
        end = clock();
   

    printf("用时%.1f秒.\n", (double)(end - start) / CLOCKS_PER_SEC);

    return 0;
}



输出结果如下:

用时0.0秒.


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-5-11 22:43:49 | 显示全部楼层
程序块执行时间够长?
#include <stdio.h>
#include <time.h>

int main() {
         clock_t start, end;
        int i;

        start = clock();

                for(i=0; i< 1000000000; i++);

        end = clock();


    printf("óÃê±%fÃë.\n", (double)(end-start)/CLOCKS_PER_SEC );

    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-11 22:45:09 | 显示全部楼层
你是不是对程序运行时间有什么误解?

你写了多复杂的算法?

1亿次简单运算用时
11112222.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-12 13:20:43 | 显示全部楼层
ba21 发表于 2021-5-11 22:43
程序块执行时间够长?

我能明白程序执行时间这个问题
但是我看明解C语言上的一个程序,打印的好像不是程序运行时间,而是用户操作用时?
WechatIMG4.jpeg

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

int main() {
    int stage;//已有数值
    int a, b, c;
    int x;
    int n;
    clock_t start, end;

    srand(time(NULL));

    printf("练习开始!!!!\n");
    start = clock();

    for (stage = 0; stage < 10; stage++) {
        a = 10 + rand()%90;
        b = 10 + rand()%90;
        c = 10 + rand()%90;

        n = rand()%17;

        printf("%d%*s+%*s%d%*s+%*s%d:", a, n, "",n,"",b,n,"",n,"",c);

        do{
            scanf("%d", &x);
            if(x == a+b+c){
                break;
            }
            printf("\a回答错误!重新输入");
        }while(1);

    }

    end = clock();
    printf("start = %d, end = %d", start, end);

    printf("用时%.1f秒.\n", (double)(end - start) / CLOCKS_PER_SEC);

    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-12 13:21:46 | 显示全部楼层
yuxijian2020 发表于 2021-5-11 22:45
你是不是对程序运行时间有什么误解?

你写了多复杂的算法?

我能明白程序执行时间这个问题
但是我看明解C语言上的一个程序,打印的好像不是程序运行时间,而是用户操作用时?


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

int main() {
    int stage;//已有数值
    int a, b, c;
    int x;
    int n;
    clock_t start, end;

    srand(time(NULL));

    printf("练习开始!!!!\n");
    start = clock();

    for (stage = 0; stage < 10; stage++) {
        a = 10 + rand()%90;
        b = 10 + rand()%90;
        c = 10 + rand()%90;

        n = rand()%17;

        printf("%d%*s+%*s%d%*s+%*s%d:", a, n, "",n,"",b,n,"",n,"",c);

        do{
            scanf("%d", &x);
            if(x == a+b+c){
                break;
            }
            printf("\a回答错误!重新输入");
        }while(1);

    }

    end = clock();
    printf("start = %d, end = %d", start, end);

    printf("用时%.1f秒.\n", (double)(end - start) / CLOCKS_PER_SEC);

    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-12 20:23:13 | 显示全部楼层
Mikein 发表于 2021-5-12 13:21
我能明白程序执行时间这个问题
但是我看明解C语言上的一个程序,打印的好像不是程序运行时间,而是用户 ...

start 和 end 中间用时。

程序执行时间 和 用户操作用时 不是一个道 理?????
有区别吗?
#include <stdio.h>
#include <time.h>

int main() {
         clock_t start, end;
        int i;

        start = clock();

                scanf("%d", &i);

        end = clock();


    printf("用时%f秒.\n", (double)(end-start)/CLOCKS_PER_SEC );

    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 08:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表