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

How to get current time in milliseconds?

本文关键字:时间 为单位      更新时间:2023-10-16

我是C 的新手,我对其库不了解。我需要对不同的排序算法进行时间分析,为此我需要在毫秒中获得当前时间。有什么方法可以做?

只需使用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;
}

而不是打印星星,而是将分类算法放在那里,然后进行时间测量。


如果您打算进行一些基准测试,例如对于G ,您需要-O3。这很严重,请检查我不这样做时发生的事情:为什么emplace_back比push_back更快?


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


使用我的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;
}

现在的输出为:

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.

那样简单。愉快的基准测试!=)


编辑:

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

来自STD :: Chrono:

时间点

对特定时间点的引用,就像一个人一样生日,今天的黎明,或下一列火车过去。在这个库,Time_point类模板的对象通过使用相对于 epoch 的持续时间(这是一个固定点使用同一时钟对所有时间_point对象共有)。

在哪里可以检查此时代和time_point示例,该示例输出:

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

简单工作示例

#include<iostream>
#include<chrono>
using namespace std;
using namespace std::chrono;
void longTask(){
    for(auto i = 0; i < INT_MAX; i++){
        //do something;
    }
}
int main(){
    auto startTime = high_resolution_clock().now();
    longTask();
    auto stopTime = high_resolution_clock().now();
    //Warning: can't print startTime or stopTime to cout withoutcasting
    //microseconds and milliseconds allowed types in duration_cast
    auto duration = duration_cast<milliseconds>(stopTime - startTime); 
    cout << "Time take to run longTask() in milliseconds: " << duration.count();
    return 0;
}

输出

时间花费毫秒:5274

运行longtask()

享受!!

您只是要求一种获得当前时间的方法,只需使用以下功能:

#include <chrono>
long double curtime() {
  return std::chrono::duration_cast<std::chrono::milliseconds>(
    std::chrono::system_clock::now().time_since_epoch()
  ).count();
}

这样使用:

int main() {
  long double begin = curtime();
  // do some crazy stuff
  long double end = curtime();
  std::cout << "Executed in " << end - begin << " milliseconds" << std::endl;
  return 0; 
}