C:Windows和其他操作系统中clock()的不同实现? [英] C: Different implementation of clock() in Windows and other OS?

查看:85
本文介绍了C:Windows和其他操作系统中clock()的不同实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为大学编写一个非常简单的控制台程序,该程序必须测量进行输入所需的时间.

I had to write a very simple console program for university that had to measure the time required to make an input.

因此,我在fgets()调用之前和之后使用clock().在我的Windows计算机上运行时,它运行完美.但是,当在我的朋友Mac-Book和Linux-PC上运行时,它给出的结果非常小(仅几微秒的时间).

Therefor I used clock() in front and after an fgets()call. When running on my Windows computer it worked perfectly. However when running on my friends Mac-Book and Linux-PC it gave extremely small results (a few micro seconds of time only).

我在所有3个操作系统上都尝试了以下代码:

I tried the following code on all 3 OS:

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

void main()
{
    clock_t t;

    printf("Sleeping for a bit\n");

    t = clock();

    // Alternatively some fgets(...)
    usleep(999999);

    t = clock() - t;

    printf("Processor time spent: %lf", ((double)t) / CLOCKS_PER_SEC);
}

在Windows上,输出显示1秒(或使用fgets时键入的时间),在其他两个OS上显示不超过0秒.

On windows the output shows 1 second (or the amount of time you took to type when using fgets), on the other two OS not much more than 0 seconds.

现在我的问题是,为什么在这些OS上实现clock()会有如此不同.对于Windows,似乎线程在睡眠/等待时时钟一直在滴答作响,但对于Linux和Mac而言却不是?

Now my question is why there is such a difference in implementation of clock() on these OS. For windows it seems like the clock keeps ticking while the thread is sleeping/waiting but for Linux and Mac isn't?

到目前为止,谢谢您提供的答案,这实际上只是Microsoft的错误实现.

Thank you for the answers so far guys, so it's just Microsoft's faulty implementation really.

任何人都可以回答我的最后一个问题:

Could anyone please answer my last question:

还有一种方法可以测量我想要使用C标准库在所有3个系统上进行测量的结果,因为clock()似乎只能在Windows上以这种方式工作?

Also is there a way to measure what I wanted do measure on all 3 systems using C-standard libraries since clock() only seems to work this way on Windows?

推荐答案

如果我们测量进程(或系统,代表进程)使用的CPU时间.这意味着,如果usleep(或fgets)使OS交换不同的程序以执行,直到发生某些事情,则经过了任何数量的实时时间(也称为挂钟",如挂钟")不计入clock()在Mac OS X上返回的值.您可能可以在Linux中进行挖掘并找到类似的内容.

If we look at the source code for clock() on Mac OS X, we see that it is implemented using getrusage, and reads ru_utime + ru_stime. These two fields measure CPU time used by the process (or by the system, on behalf of the process). This means that if usleep (or fgets) causes the OS to swap in a different program for execution until something happens, then any amount of real time (also called "wall time", as in "wall clock") elapsed does not count against the value that clock() returns on Mac OS X. You could probably dig in and find something similar in Linux.

但是,在Windows上,clock() 返回的是挂墙时间 a>自该过程开始以来已经过去.

On Windows, however, clock() returns the amount of wall time elapsed since the start of the process.

在纯C语言中,我不知道OS X,Linux和Windows上可用的函数会以不到一秒的精度返回壁挂时间(time.h相当有限).您有 GetSystemTimeAsFileTime 在Windows上将以100ns为单位返回您的时间,并且 gettimeofday 来自BSD,它将使时间返回毫秒级.

In pure C, I am not aware of a function available on OS X, Linux and Windows that will return wall time with a sub-second precision (time.h being fairly limited). You have GetSystemTimeAsFileTime on Windows that will return you time in slices of 100ns, and gettimeofday from BSD that will return time to a microsecond precision.

如果您可以接受第二精度,则可以使用 time(NULL) .

If second-precision is acceptable to you, you could use time(NULL).

如果可以选择使用C ++,则可以使用 std::chrono 中的一个时钟使时间达到所需的精度.

If C++ is an option, you could use one of the clocks from std::chrono to get time to the desired precision.

这篇关于C:Windows和其他操作系统中clock()的不同实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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