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

时间:2021-07-08 01:25:42

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++新手,对它的库了解不多。我需要对不同的排序算法进行时间分析,我需要得到当前时间(毫秒)。有什么办法吗?

1 个解决方案

#1


16  

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

简单地使用std::空间。一般的例子是下面乘以“打印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 , 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 , then you could look into other methods in my Time Measurements (C++).

如果您的编译器不支持c++11,那么您可以在我的时间度量(c++)中查看其他方法。


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

一个特定的(玩具)例子,通过使用我的快速排序(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?

函数的作用是:现在()返回相对于哪个时间的时间?

From std::chrono:

从std::空间:

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

#1


16  

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

简单地使用std::空间。一般的例子是下面乘以“打印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 , 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 , then you could look into other methods in my Time Measurements (C++).

如果您的编译器不支持c++11,那么您可以在我的时间度量(c++)中查看其他方法。


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

一个特定的(玩具)例子,通过使用我的快速排序(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?

函数的作用是:现在()返回相对于哪个时间的时间?

From std::chrono:

从std::空间:

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