c - 获取程序中函数的真实时间、用户时间、系统时间

标签 c function time timing

寻求帮助以获取我的 C 程序中函数的实际、用户、系统时间。例如读取一个文件需要多长时间。我一直在查看 #include 并使用带有 -p 标志的 time() 函数,但我对它的执行感到震惊。我想我的问题是我是否有:

time_t total_time;
time_t start, end;

start = time(-p, &start);

<some code>

end = time(-p, &end);

printf("real = %e, user = %S, sys = %S\n", ???????????);

我了解这三个时间之间的差异,只是不知道获取结果的正确执行方式。

最佳答案

检查time()的返回值:

The value returned generally represents the number of seconds since 00:00 hours, Jan 1, 1970 UTC (i.e., the current unix timestamp). Although libraries may use a different representation of time: Portable programs should not use the value returned by this function directly, but always rely on calls to other elements of the standard library to translate them to portable types (such as localtime, gmtime or difftime).

要显示实时时间,请阅读此 answer .

对于用户和系统,请阅读:How to measure user/system cpu time for a piece of program?

关于c - 获取程序中函数的真实时间、用户时间、系统时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39650317/

相关文章:

c++ - 使用 Windows API 查找路径类型

c++ - 具有函数默认值的参数分配

python - 如何从文本文件中的特定行获取计时并用Python中的另一行减去它?

iphone - 如何控制 UISlider Value Changed-events 频率?

android - TalkBack 读取时间错误

c - openSSL 示例中的 SSL 接受错误

c++ - 为什么,如果一个 char 被初始化为 1,然后左移 7 次并使用 %d 打印值,它会显示 -128 吗?

c - ARM Cortex M4 上的数组大小问题

function - golang中变量的范围

c++ - 如何使函数接受任意数量的参数而不使用 f(...)?