我尝试计算通过循环做了多少次操作1秒。 为此,我记得当我开始计算循环并在每次迭代中检查时间时。 我的想法 - 当这两个timemoments的秒数不同时,我printf做了多少循环迭代。错误与参考C++
这里是我的代码:
#include <ctime>
int main()
{
// For timing
time_t t, tstep;
struct tm* now, *step;
// this time will change at every iteration
t = time(0);
now = localtime(&t);
// save time of the start moment
tstep = t;
step = localtime(&tstep);
// counter of loop cycles
int count = 0;
for (size_t i = 0; i < 1e100 ; i++)
{
// ... here is some calculations
t = time(0);
now = localtime(&t);
count++;
if (now->tm_sec != step->tm_sec)
{
tstep = time(0);
step = localtime(&tstep);
//printf("number of lines %i \n", count);
count = 0;
}
}
return 0;
}
是什么问题:我每次刷新now
step
成为相同的值!而t
和tstep
是不同的!
看起来像这是因为引用:也许当我使用tstep = t
这意味着这个变量的地址是指t
都。因此更改t
更改now
和tstep
。
如何解决这个问题?如何将t
的值复制到step
?或者还有另一种实际的方式?
如果你downvote,你能解释为什么吗? –
“_像这样看是因为参考_”你是**不**在代码中的任何地方使用引用。 –
此外,这不是[最小,完整和可验证的示例](https://stackoverflow.com/help/mcve) – kim366