C++ 中的时间不正确 [英] Incorrect Time in C++

查看:39
本文介绍了C++ 中的时间不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

clock_t tt = clock();
sleep(10);
tt = clock()-tt;
cout<<(float)tt/CLOCKS_PER_SEC<<" "<<CLOCKS_PER_SEC<<endl;

当我运行代码时,它显然暂停了 10 秒,输出为:

When I run the code, it apparently pauses for 10 seconds and the output is:

0.001074 1000000

0.001074 1000000

这表明它通过了 1074 个时钟滴答和 1ms,这显然是错误的.

This indicates it passed 1074 clock ticks and 1ms, which is apparently false.

为什么会发生这种情况?

Why does this happen?

我在 linux 下使用 g++.

I am using g++ under linux.

推荐答案

clocks 函数返回程序消耗的处理器时间.睡眠时,您的进程不使用任何处理量,因此这是意料之中的.您的程序显示的时间可能来自 clock 函数调用.

The function clocks returns the processor time consumed by the program. While sleeping, your process does not use any amount of processing, so this is expected. The amount of time your program is showing could be from the clock function calling.

这篇关于C++ 中的时间不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆