[英]How can I make my own timer without standard library?
Is there some specific number of iterations, that I could make using a for loop
, so that it exactly takes 1 second for the loop to be executed completely? 是否有一些特定的迭代次数,我可以使用for loop
,因此完全执行循环需要1秒钟? For example the following code took 0.125s on my machine to execute: 例如,以下代码在我的机器上执行了0.125秒:
#include <iostream>
#include <cmath>
using namespace std;
int main(){
long long a=0;
for (a=0;a<=pow(10,4);a++);
}
Though, a <= 8*pow(10,4) took 0.206 s. 虽然,<= 8 * pow(10,4)需要0.206秒。 Compiler is GCC 4.9.2. 编译器是GCC 4.9.2。 IDE is codeblocks. IDE是代码块。
My PC's Specs: OS: Windows 8.1 我的电脑的规格:操作系统:Windows 8.1
I am posting this answer to your question, as per the comments received. 根据收到的评论,我将这个答案发布到您的问题中。
It is not possible to make a timer because: 制作计时器是不可能的,因为:
one would have to use a real time OS to accomplish that. 人们将不得不使用实时操作系统来实现这一目标。 There's too much jitter in non realtime OSs. 非实时操作系统中存在太多抖动。 Windows could decide to schedule other processes for a while, or use the CPU for eg kernel networking, disk I/O etc. that preempts the timing. Windows可以决定暂时安排其他进程,或者将CPU用于例如内核网络,磁盘I / O等优先于时间的CPU。 (By nos) (由nos)
One can't "make own timer" in a hosted environment just in standard C++. 人们不能仅仅在标准C ++中在托管环境中“制作自己的计时器”。 A timer is essentially a mechanism to communicate with the OS scheduler, and one needs platform-specific OS services for that. 定时器本质上是一种与OS调度程序通信的机制,并且需要特定于平台的OS服务。 (By Kerrek SB) (由Kerrek SB提供)
The compiler would optimize such a loop and will remove it through dead-code elimination (By πάντα ῥεῖ and Jongware). 编译器将优化这样的循环并通过死代码消除(通过πάνταῥεῖ和Jongware)将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.