C++ 如何以毫秒为单位获取当前时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41077377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:25:34  来源:igfitidea点击:

How to get current time in milliseconds?

c++algorithmperformancetimestl

提问by Yasir Mustafa

I am new to C++ and I don't know much about its library. I need to do time analysis of different sorting algorithms, for which I need to get the current time in milliseconds. Is there any way to do that?

我是 C++ 的新手,我对它的库知之甚少。我需要对不同的排序算法进行时间分析,为此我需要以毫秒为单位获取当前时间。有没有办法做到这一点?

回答by gsamaras

Simply use std::chrono. The general example bellow times the task "of printing 1000 stars":

只需使用std::chrono。下面的一般示例将“打印 1000 颗星”的任务放大了:

#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>

int main ()
{
  using namespace std::chrono;

  high_resolution_clock::time_point t1 = high_resolution_clock::now();

  std::cout << "printing out 1000 stars...\n";
  for (int i=0; i<1000; ++i) std::cout << "*";
  std::cout << std::endl;

  high_resolution_clock::time_point t2 = high_resolution_clock::now();

  duration<double, std::milli> time_span = t2 - t1;

  std::cout << "It took me " << time_span.count() << " milliseconds.";
  std::cout << std::endl;

  return 0;
}

Instead of printing the stars, you will place your sorting algorithm there and time measure it.

您无需打印星星,而是将排序算法放在那里并进行时间测量。



Do not forget to enable the optimization flags for your compiler, if you intend to do some benchmarking, e.g. for g++, you need -O3. This is serious, check what happened to me when I didn't do so: Why emplace_back is faster than push_back?

不要忘记为您的编译器启用优化标志,如果您打算进行一些基准测试,例如对于g++,您需要-O3. 这很严重,检查我没有这样做时发生了什么:为什么 emplace_back 比 push_back 快?



Ps: If your compiler doesn't support c++11, then you could look into other methods in my Time Measurements (C++).

Ps:如果您的编译器不支持c++11,那么您可以查看我的Time Measurements (C++) 中的其他方法。



A specific (toy) example, by using my Quicksort (C++), would be:

一个特定的(玩具)示例,通过使用我的Quicksort (C++),将是:

#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>

void quickSort(int a[], int first, int last);
int pivot(int a[], int first, int last);
void swap(int& a, int& b);
void swapNoTemp(int& a, int& b);

using namespace std;
using namespace std::chrono;

int main()
{
    int test[] = { 7, -13, 1, 3, 10, 5, 2, 4 };
    int N = sizeof(test)/sizeof(int);

    cout << "Size of test array :"  << N << endl;

    high_resolution_clock::time_point t1 = high_resolution_clock::now();

    // I want to measure quicksort
    quickSort(test, 0, N-1);

    high_resolution_clock::time_point t2 = high_resolution_clock::now();

    duration<double> time_span = t2 - t1;

    std::cout << "It took me " << time_span.count() << " seconds.";
    std::cout << std::endl;

    return 0;
}

and the output now is:

现在的输出是:

Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall -std=c++11 -O3 main.cpp 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
Size of test array :8
It took me 3.58e-07 seconds.

It's as simple as that. Happy benchmarking! =)

就这么简单。快乐的基准测试!=)



EDIT:

编辑:

high_resolution_clock::now()function returns time relative to which time?

high_resolution_clock::now()函数返回相对于哪个时间的时间?

From std::chrono:

std::chrono

Time points

A reference to a specific point in time, like one's birthday, today's dawn, or when the next train passes. In this library, objects of the time_point class template express this by using a duration relative to an epoch(which is a fixed point in time common to all time_point objects using the same clock).

时间点

对特定时间点的引用,例如生日、今天的黎明或下一趟火车经过的时间。在这个库中,time_point 类模板的对象通过使用相对于纪元的持续时间(这是使用相同时钟的所有 time_point 对象共有的固定时间点)来表达这一点。

where one could check this epoch and time_point example, which outputs:

在哪里可以检查这个epoch 和 time_point 示例,它输出:

time_point tp is: Thu Jan 01 01:00:01 1970