std :: chrono :: high

编程入门 行业动态 更新时间:2024-10-22 23:41:26
本文介绍了std :: chrono :: high_resolution_clock不精确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我试图使用std :: chrono :: high_resolution_clock来计算需要执行多长时间。我想你可以找到开始时间和结束时间之间的差异...

So I was trying to use std::chrono::high_resolution_clock to time how long something takes to executes. I figured that you can just find the difference between the start time and end time...

为了检查我的方法工作,我做了以下程序:

To check my approach works, I made the following program:

#include <iostream> #include <chrono> #include <vector> void long_function(); int main() { std::chrono::high_resolution_clock timer; auto start_time = timer.now(); long_function(); auto end_time = timer.now(); auto diff_millis = std::chrono::duration_cast<std::chrono::duration<int, std::milli>>(end_time - start_time); std::cout << "It took " << diff_millis.count() << "ms" << std::endl; return 0; } void long_function() { //Should take a while to execute. //This is calculating the first 100 million //fib numbers and storing them in a vector. //Well, it doesn't actually, because it //overflows very quickly, but the point is it //should take a few seconds to execute. std::vector<unsigned long> numbers; numbers.push_back(1); numbers.push_back(1); for(int i = 2; i < 100000000; i++) { numbers.push_back(numbers[i-2] + numbers[i-1]); } }

问题是,它只输出3000ms它显然不是真的。

The problem is, it just outputs 3000ms exactly, when it clearly wasn't actually that.

对于较短的问题,它只输出0ms ...我做错了什么?

On shorter problems, it just outputs 0ms... What am I doing wrong?

编辑:如果它是任何用途,我使用GNU GCC编译器与

If it's of any use, I'm using the GNU GCC compiler with -std=c++0x flag on

推荐答案

high_resolution_clock的分辨率取决于平台。

The resolution of the high_resolution_clock depends on the platform.

打印以下内容可以帮助您了解您使用的实现的解决方案。

Printing the following will give you an idea of the resolution of the implementation you use

std::cout << "It took " << std::chrono::nanoseconds(end_time - start_time).count() << std::endl;

更多推荐

std :: chrono :: high

本文发布于:2023-11-27 04:26:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636587.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:std   chrono   high

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!