gpt4 book ai didi

c++ - 为什么我的第二个 time_t 变量没有正确显示

转载 作者:行者123 更新时间:2023-11-27 22:34:37 26 4
gpt4 key购买 nike

我想测量一个特定操作需要多长时间,因此我编写了以下代码:

for (int counter2 = 0; counter2 <= 10; counter2++) { // I'll do the test 10 times,
// for having reliable results

time_t ltime;
time(&ltime); // What's the time at the beginning?

for (counter = 0; counter <= 1000000; counter++) {
Do_Something();
}

time_t ltime2;
time(&ltime2); // What's the time after having launched Do_Something()?
printf("Times:First=[%d], Second=[%d]\n", ltime, ltime2);

}

我期待这样的事情:

Times: First=[1559574983], Second=[1559574985]
Times: First=[1559574990], Second=[1559574999]

相反,我得到了这个:

Times: First=[1559574983], Second=[0]
Times: First=[1559574990], Second=[0]

我已经调试过了,ltime2似乎是正确的。我做错了什么?

最佳答案

格式"%d"在你的情况下不是正确的,但你不必照顾支持 time_t 的类型只是更换

 printf("Times:First=[%d], Second=[%d]\n", ltime, ltime2);

通过

std::cout << "Times:First=[" << ltime << "], Second=[" << ltime2 << "]" << std::endl;

如果出于某种不明确的原因你真的想使用@Lightness Races in Orbit 3 在评论中提出的 C printf 你可以将该值转换为其他类型(跳跃足够长)并使用与该类型兼容的格式,请参阅 Format specifier to print time(0) in C

关于c++ - 为什么我的第二个 time_t 变量没有正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56430278/

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