如何在 C 语言中获取程序的执行时间(以毫秒为单位)?

发布于 2024-08-20 04:20:16 字数 253 浏览 1 评论 0原文

目前,我通过调用以为单位获取程序的执行挂起时间:

time_t startTime = time(NULL);
//section of code
time_t endTime = time(NULL);
double duration = difftime(endTime, startTime);

是否可以以毫秒为单位获取挂起时间?如果是这样怎么办?

Currently I'm getting the execution wall time of my program in seconds by calling:

time_t startTime = time(NULL);
//section of code
time_t endTime = time(NULL);
double duration = difftime(endTime, startTime);

Is it possible to get the wall time in milliseconds? If so how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

白昼 2024-08-27 04:20:17

我最近写了一篇博客文章,解释 如何跨平台获取以毫秒为单位的时间

它的工作方式与 time(NULL) 类似,但在 Windows 和 Linux 上都会返回 UNIX 纪元的毫秒数而不是秒数。

这是代码,

#ifdef WIN32
#include <Windows.h>
#else
#include <sys/time.h>
#include <ctime>
#endif

/* Returns the amount of milliseconds elapsed since the UNIX epoch. Works on both
 * windows and linux. */

int64 GetTimeMs64()
{
#ifdef WIN32
 /* Windows */
 FILETIME ft;
 LARGE_INTEGER li;
 uint64 ret;

 /* Get the amount of 100 nano seconds intervals elapsed since January 1, 1601 (UTC) and copy it
  * to a LARGE_INTEGER structure. */
 GetSystemTimeAsFileTime(&ft);
 li.LowPart = ft.dwLowDateTime;
 li.HighPart = ft.dwHighDateTime;

 ret = li.QuadPart;
 ret -= 116444736000000000LL; /* Convert from file time to UNIX epoch time. */
 ret /= 10000; /* From 100 nano seconds (10^-7) to 1 millisecond (10^-3) intervals */

 return ret;
#else
 /* Linux */
 struct timeval tv;
 uint64 ret;

 gettimeofday(&tv, NULL);

 ret = tv.tv_usec;
 /* Convert from micro seconds (10^-6) to milliseconds (10^-3) */
 ret /= 1000;

 /* Adds the seconds (10^0) after converting them to milliseconds (10^-3) */
 ret += (tv.tv_sec * 1000);

 return ret;
#endif
}

如果需要,您可以修改它以返回微秒而不是毫秒。

I recently wrote a blog post that explains how to obtain the time in milliseconds cross-platform.

It will work like time(NULL), but will return the number of milliseconds instead of seconds from the unix epoch on both windows and linux.

Here is the code

#ifdef WIN32
#include <Windows.h>
#else
#include <sys/time.h>
#include <ctime>
#endif

/* Returns the amount of milliseconds elapsed since the UNIX epoch. Works on both
 * windows and linux. */

int64 GetTimeMs64()
{
#ifdef WIN32
 /* Windows */
 FILETIME ft;
 LARGE_INTEGER li;
 uint64 ret;

 /* Get the amount of 100 nano seconds intervals elapsed since January 1, 1601 (UTC) and copy it
  * to a LARGE_INTEGER structure. */
 GetSystemTimeAsFileTime(&ft);
 li.LowPart = ft.dwLowDateTime;
 li.HighPart = ft.dwHighDateTime;

 ret = li.QuadPart;
 ret -= 116444736000000000LL; /* Convert from file time to UNIX epoch time. */
 ret /= 10000; /* From 100 nano seconds (10^-7) to 1 millisecond (10^-3) intervals */

 return ret;
#else
 /* Linux */
 struct timeval tv;
 uint64 ret;

 gettimeofday(&tv, NULL);

 ret = tv.tv_usec;
 /* Convert from micro seconds (10^-6) to milliseconds (10^-3) */
 ret /= 1000;

 /* Adds the seconds (10^0) after converting them to milliseconds (10^-3) */
 ret += (tv.tv_sec * 1000);

 return ret;
#endif
}

You can modify it to return microseconds instead of milliesconds if you want.

孤城病女 2024-08-27 04:20:17

开源 GLib 库有一个 GTimer 系统,声称可以提供微秒级的精度。该库可在 Mac OS X、Windows 和 Linux 上使用。我目前正在使用它在 Linux 上进行性能计时,它似乎工作得很好。

The open-source GLib library has a GTimer system that claims to provide microsecond accuracy. That library is available on Mac OS X, Windows, and Linux. I'm currently using it to do performance timings on Linux, and it seems to work perfectly.

涫野音 2024-08-27 04:20:17

gprof 是 GNU 工具包的一部分,是一个选项。大多数 POSIX 系统都会安装它,并且可以在 Windows 的 Cygwin 下使用它。使用 gettimeofday() 自己跟踪时间效果很好,但其性能相当于使用 print 语句进行调试。如果您只是想要一个快速而肮脏的解决方案,那么这很好,但它不如使用适当的工具那么优雅。

要使用gprof,您必须在使用gcc编译时指定-pg选项,如下所示:

gcc -o prg source.c -pg

然后您可以在生成的程序上运行gprof,如下所示:

gprof prg > gprof.out

默认情况下,gprof 会生成你的程序的整体运行时间,以及每个函数所花费的时间量、每个函数被调用的次数、每个函数调用所花费的平均时间以及类似的信息。

您可以使用 gprof 设置大量选项。如果您有兴趣,可以在手册页或通过 Google 获取更多信息。

gprof, which is part of the GNU toolkit, is an option. Most POSIX systems will have it installed, and it's available under Cygwin for Windows. Tracking the time yourself using gettimeofday() works fine, but it's the performance equivalent of using print statements for debugging. It's good if you just want a quick and dirty solution, but it's not quite as elegant as using proper tools.

To use gprof, you must specify the -pg option when compiling with gcc as in:

gcc -o prg source.c -pg

Then you can run gprof on the generated program as follows:

gprof prg > gprof.out

By default, gprof will generate the overall runtime of your program, as well as the amount of time spent in each function, the number of times each function was called, the average time spent in each function call, and similar information.

There are a large number of options you can set with gprof. If you're interested, there is more information in the man pages or through Google.

欲拥i 2024-08-27 04:20:17

在 Windows 上,使用 QueryPerformanceCounter 和关联的 QueryPerformanceFrequency。它们不会为您提供可转换为日历时间的时间,因此,如果您需要,请使用 CRT API 请求时间,然后立即使用 QueryPerformanceCounter。然后,您可以进行一些简单的加法/减法来计算日历时间,但由于连续执行 API 所需的时间,会出现一些错误。嘿,这是一台电脑,你期待什么???

On Windows, use QueryPerformanceCounter and the associated QueryPerformanceFrequency. They don't give you a time that is translatable to calendar time, so if you need that then ask for the time using a CRT API and then immediately use QueryPerformanceCounter. You can then do some simple addition/subtraction to calculate the calendar time, with some error due to the time it takes to execute the API's consecutively. Hey, it's a PC, what did you expect???

破晓 2024-08-27 04:20:16

如果您使用的是 POSIX 式机器,请使用 gettimeofday()< /code>相反;这为您提供了合理的便携性和微秒分辨率。

稍微深奥一些,但也在 POSIX 中,是 clock_gettime()函数,它为您提供纳秒分辨率。

在许多系统上,您会发现一个函数ftime(),它实际上返回以秒和毫秒为单位的时间。然而,它不再出现在单一 Unix 规范中(与 POSIX 大致相同)。您需要标头

struct timeb mt;
if (ftime(&mt) == 0)
{
     mt.time ... seconds
     mt.millitime ... milliseconds
}

这至少可以追溯到版本 7(或第七版)Unix,因此它已被广泛使用。

我还在我的亚秒计时器代码中对 times()clock() 进行了注释,它们再次使用其他结构和标头。我还有关于 Windows 使用 clock() 的注释,每秒 1000 个时钟周期(毫秒计时),以及旧的接口 GetTickCount()(在 Windows 95 上有必要注明)但在新台币上则不然。

If you're on a POSIX-ish machine, use gettimeofday() instead; that gives you reasonable portability and microsecond resolution.

Slightly more esoteric, but also in POSIX, is the clock_gettime() function, which gives you nanosecond resolution.

On many systems, you will find a function ftime() that actually returns you the time in seconds and milliseconds. However, it is no longer in the Single Unix Specification (roughly the same as POSIX). You need the header <sys/timeb.h>:

struct timeb mt;
if (ftime(&mt) == 0)
{
     mt.time ... seconds
     mt.millitime ... milliseconds
}

This dates back to Version 7 (or 7th Edition) Unix at least, so it has been very widely available.

I also have notes in my sub-second timer code on times() and clock(), which use other structures and headers again. I also have notes about Windows using clock() with 1000 clock ticks per second (millisecond timing), and an older interface GetTickCount() which is noted as necessary on Windows 95 but not on NT.

你穿错了嫁妆 2024-08-27 04:20:16

如果您可以在程序本身之外执行此操作,那么在 Linux 中,您可以使用 time 命令 (time ./my_program)。

If you can do this outside of the program itself, in linux, you can use time command (time ./my_program).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文