clock()在C中返回负值 [英] clock() returning a negative value in C

查看:90
本文介绍了clock()在C中返回负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个非常简单的代码来衡量执行时间.它可以很好地工作直到我不确定可能不超过20分钟.但是(> 20min.)之后它返回了负结果.论坛,并尝试使用长无符号(返回0)更改数据类型之类的所有方法,但再次失败.以下是我的代码段

I'm using a quite simple code to measure for time of execution.It works well until I am not sure may be not more than 20 minutes.But after(>20min.)it is returning negative results.I searched throughout the forums and tried everything like changing the datatype,using long unsigned (which is returning 0) but failed again. The following is the snippet of my code

main()
{
    time_t start,stop;
    double time_arm;
    start = clock(); 
    /* .......  */
    stop = clock();
    time_arm=(double)(stop-start)/(double)CLOCKS_PER_SEC;

    printf("Time Taken by ARM only is %lf \n",time_arm);
}

输出为仅ARM占用的时间为 -2055.367296

output is Time Taken by ARM only is -2055.367296

感谢您的帮助,谢谢.

推荐答案

POSIX要求 CLOCKS_PER_SEC 为1,000,000.这意味着您的计数以微秒为单位-2 31 微秒约为35分钟.您的计时器刚刚溢出,因此发生这种情况时您将无法获得有意义的结果.

POSIX requires CLOCKS_PER_SEC to be 1,000,000. That means your count is in microseconds - and 231 microseconds is about 35 minutes. Your timer is just overflowing, so you can't get meaningful results when that happens.

为什么在20分钟后看到问题,我不确定-也许您的 CLOCKS_PER_SEC 与POSIX不兼容.无论如何,您的问题是计时器溢出.您将需要以不同的方式来处理此问题-也许要研究 getrusage(2) .

Why you see the problem at 20 minutes, I'm not sure - maybe your CLOCKS_PER_SEC isn't POSIX-compatible. Regardless, your problem is timer overflow. You'll need to handle this problem in a different way - maybe look into getrusage(2).

这篇关于clock()在C中返回负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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