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

新手上路,请多包涵

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

原文由 Yasir Mustafa 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 478
2 个回答

只需使用 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 ,那么您可以查看我的 Time Measurements (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 类模板的对象通过使用相对于 纪元 的持续时间(这是使用同一时钟的所有 time_point 对象共有的固定时间点)来表达这一点。

可以检查这个 epoch 和 time_point example ,它输出:

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

原文由 gsamaras 发布,翻译遵循 CC BY-SA 4.0 许可协议

简单的工作示例

#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;
}

输出

以毫秒为单位运行 longTask() 的时间:5274

享受 !!

原文由 Om Sao 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题