如何以毫秒为单位获得当前时间
How to get current time in milliseconds?
我是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;
}
相关文章:
- 以天C++为单位的两个时间戳之间的差异
- 如何以毫秒为单位获取开始时间和 now() 之间的毫秒差异(以 C++为单位?
- 如何找到两个日期之间的时间差异(以秒和纳秒为单位)?
- 测量精确的时间,单位为纳秒C++
- 如何在python和C++中以毫秒为单位打印相同的时间
- C++ 旧时间到现在的时间(以小时为单位)
- 如何找到纪元时间戳和 std::chrono::system_clock::now 之间的时差(以毫秒为单位)
- 仅使用C++中的标准库获取以毫秒为单位的当前日期和时间
- 以秒为单位将时间从纪元转换为一年中的某一天?
- 如何使用闰秒以微秒为单位获取 UTC 时间
- 测量 std::系统的实际执行时间(以 C++ 为单位)
- 优先级队列的时间复杂度(以C++为单位)
- C++以微秒为单位解析日期/时间
- 以C++为单位的聚合代码块的墙时间
- 使用 c++ 获取两个日期与时间字符串之间的差异(以秒为单位)
- 如何在跨平台C++中以毫秒为单位获得系统启动/启动时间(它应该在Windows / IOS / Android / MA
- 以 ns3 为单位计算数据包接收时间
- 如何在C++中以毫秒为单位获取今天的 UTC 午夜时间?
- 计算以毫秒为单位的分类算法对整数阵列排序的时间
- 如何以毫秒为单位获得当前时间