简体   繁体   English

如何在没有标准库的情况下制作自己的计时器?

[英]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: 制作计时器是不可能的,因为:

  • The time that an iteration will take is unpredictable, this depends not only on the CPU used, but you need to take into account power management, the scheduler. 迭代将花费的时间是不可预测的,这不仅取决于所使用的CPU,还需要考虑电源管理(调度程序)。 (By tux3) (通过tux3)
  • 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.

相关问题 如何为我自己的 class 专门设计一个标准概念? - How can I specialize a standard concept for my own class? 如何使用我从源代码构建的库而没有错误,但不能为我自己的项目编译? - How can I use the library that I have built without error from source, but not compiling for my own project? 如何从标准流中获取自己的流? - How can I derive my own stream from a standard stream? 如何制作自己的piecewise_construct构造函数? - How can I make my own piecewise_construct constructor? 如何制作多人Flash游戏? 有什么套接字库/我如何自己创建? - How can I make a multiplayer Flash game? What socket libraries are there/how do I make my own? C ++如何在不使用标准库中的随机播放功能的情况下随机播放矢量? - C++ How can I shuffle a vector without using the shuffle functions from the standard library? 如何将标准库静态链接到我的 C++ 程序? - How can I statically link standard library to my C++ program? 如何使 OpenCV 成为我的 qt 项目的默认库? - How can I make OpenCV the default library for my qt projects? 如何在我的 dll 接口或 ABI 中使用标准库 (STL) 类? - How can I use Standard Library (STL) classes in my dll interface or ABI? 如何使boost / asio库重复计时器? - How do I make the boost/asio library repeat a timer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM