time.h clock()在OS X上坏了吗? [英] time.h clock() broken on OS X?

查看:142
本文介绍了time.h clock()在OS X上坏了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会生气吗?我正在x86_64上运行它.

Am I going mad? I'm running this on x86_64.

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

int main(int argc, char *argv[])
{
    printf("Clock: %f\n", clock() / (double)CLOCKS_PER_SEC);
    sleep(1);
    printf("Clock: %f\n", clock() / (double)CLOCKS_PER_SEC);
    sleep(1);
    printf("Clock: %f\n", clock() / (double)CLOCKS_PER_SEC);
    return 0;
}

此打印

时钟:0.002880
时钟:0.002968
时钟:0.003019

Clock: 0.002880
Clock: 0.002968
Clock: 0.003019

它显然在sleep(1)行上等待一秒钟,但是输出显然是错误的.

It clearly waits for a second at the sleep(1) lines, but the output is clearly wrong.

如果这不起作用,是否有便携式C替代品?

If that doesn't work, is there a portable C alternative?

推荐答案

我是个白痴. clock()返回已使用的处理器时间.

I'm an idiot. clock() returns processor time used.

这篇关于time.h clock()在OS X上坏了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆