gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:37:34 24 4
gpt4 key购买 nike

寻求帮助以获取我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com