如何将 std::chrono::high_resolution_clock::now() 转换为毫秒、微秒、...?
How to convert std::chrono::high_resolution_clock::now() to milliseconds, microseconds, ...?
我从 How to get duration, as int milli's and float seconds from <chrono>?
那里得到了这段代码
#include <chrono>
#include <iostream>
int main (int argc, char *argv[])
{
auto t0 = std::chrono::high_resolution_clock::now();
auto t1 = std::chrono::high_resolution_clock::now();
std::chrono::duration< double > fs = t1 - t0;
std::chrono::milliseconds d = std::chrono::duration_cast< std::chrono::milliseconds >( fs );
std::cout << fs.count() << "s\n";
std::cout << d.count() << "ms\n";
}
效果很好,但我如何创建时间戳:
hour:minute:second:millisecond:microsecond:nanosecond
使用 auto t0 = std::chrono::high_resolution_clock::now()
值?
我试图打印 auto t0 = std::chrono::high_resolution_clock::now();
值,看看里面有什么,但它只给我一个很大的错误堆栈:
#include <chrono>
#include <iostream>
int main (int argc, char *argv[])
{
auto t0 = std::chrono::high_resolution_clock::now();
std::cout << t0 << "\n";
}
错误:
main2.cpp: In function 'int main(int, char**)':
main2.cpp:10:13: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long long int, std::ratio<1, 1000000000> > >')
std::cout << t0 << "\n";
~~~~~~~~~~^~~~~
感谢@Miles Budnek comment, I cannot use high_resolution_clock
because it does not measure time, but CPU ticks. So, I found this answer based on Print current system time in nanoseconds using c++ chrono做到最好。
#include <chrono>
#include <ctime>
#include <time.h>
#include <iostream>
// C++ -> Numerics library -> Compile time rational arithmetic -> std::ratio
// http://en.cppreference.com/w/cpp/numeric/ratio/ratio
//
// How to convert std::chrono::high_resolution_clock::now() to milliseconds, microseconds, ...?
//
int main (int argc, char *argv[])
{
std::chrono::time_point< std::chrono::system_clock > now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();
/* UTC: -3:00 = 24 - 3 = 21 */
typedef std::chrono::duration< int, std::ratio_multiply< std::chrono::hours::period, std::ratio< 21 > >::type > Days;
Days days = std::chrono::duration_cast< Days >( duration );
duration -= days;
auto hours = std::chrono::duration_cast< std::chrono::hours >( duration );
duration -= hours;
auto minutes = std::chrono::duration_cast< std::chrono::minutes >( duration );
duration -= minutes;
auto seconds = std::chrono::duration_cast< std::chrono::seconds >( duration );
duration -= seconds;
auto milliseconds = std::chrono::duration_cast< std::chrono::milliseconds >( duration );
duration -= milliseconds;
auto microseconds = std::chrono::duration_cast< std::chrono::microseconds >( duration );
duration -= microseconds;
auto nanoseconds = std::chrono::duration_cast< std::chrono::nanoseconds >( duration );
// C library function - localtime()
// https://www.tutorialspoint.com/c_standard_library/c_function_localtime.htm
//
// struct tm {
// int tm_sec; // seconds, range 0 to 59
// int tm_min; // minutes, range 0 to 59
// int tm_hour; // hours, range 0 to 23
// int tm_mday; // day of the month, range 1 to 31
// int tm_mon; // month, range 0 to 11
// int tm_year; // The number of years since 1900
// int tm_wday; // day of the week, range 0 to 6
// int tm_yday; // day in the year, range 0 to 365
// int tm_isdst; // daylight saving time
// };
time_t theTime = time(NULL);
struct tm *aTime = localtime(&theTime);
std::cout << days.count() << " days since epoch or "
<< days.count() / 365.2524 << " years since epoch. The time is now "
<< aTime->tm_hour << ":"
<< minutes.count() << ":"
<< seconds.count() << ":"
<< milliseconds.count() << ":"
<< microseconds.count() << ":"
<< nanoseconds.count() << std::endl;
}
运行 它,输出:
$ g++ -O0 -g -Wall -std=c++11 -o test timestamp_example.cpp && ./test
20107 days since epoch or 55.0496 years since epoch. The time is now 21:39:51:935:732:700
我有一个 class,用于使用 std::chrono
进行基准计时计算。它是一个接受决议的 class 模板。此处显示:
#ifndef EXECUTION_TIMER_H
#define EXECUTION_TIMER_H
#include <chrono>
#include <type_traits>
#include <sstream>
#include <iostream>
template <class Resolution = std::chrono::milliseconds>
class ExecutionTimer {
public:
using Clock = std::conditional_t<std::chrono::high_resolution_clock::is_steady,
std::chrono::high_resolution_clock,
std::chrono::steady_clock>;
private:
const Clock::time_point mStart = Clock::now();
public:
ExecutionTimer() = default;
~ExecutionTimer() {
const auto end = Clock::now();
std::ostringstream strStream;
strStream << "Destructor Elapsed: "
<< std::chrono::duration_cast<Resolution>(end - mStart).count()
<< std::endl;
std::cout << strStream.str() << std::endl;
}
inline void stop() {
const auto end = Clock::now();
std::ostringstream strStream;
strStream << "Stop Elapsed: "
<< std::chrono::duration_cast<Resolution>(end - mStart).count()
<< std::endl;
std::cout << strStream.str() << std::endl;
}
};
#endif // !EXECUTION_TIMER_H
使用 class 非常简单,因为驱动程序将演示。
#include <vector>
#include <iostream>
#include <conio.h> // for _getch().
#include "ExecutionTimer.h"
void someFunc() {
// Some Operations Just To Get Difference In Time
std::vector<int> values;
for ( unsigned int i = 0; i < 1000; i++ ) {
values.push_back( i * 2 );
}
int printed = 0;
for ( auto i : values ) {
if ( printed % 10 == 0 ) {
std::cout << "\n";
std::cout << i << ", ";
} else {
std::cout << i << ", ";
}
printed++;
}
std::cout << std::endl;
}
int main() {
ExecutionTimer<> timer; // Default = std::chrono::miliseconds
someFunc();
timer.stop();
ExecutionTimer<std::chrono::microseconds> timer2;
someFunc();
timer2.stop();
_getch();
return 0;
}
上面的 class 正在以某种方式做我认为是你所要求的。
现在,在查看上面的代码时,尤其是此处的这些行:
std::chrono::duration< double > fs = t1 - t0;
std::chrono::milliseconds d = std::chrono::duration_cast< std::chrono::milliseconds >( fs );
我相信您在将值传递给 duration_cast<>
后遗漏了 .count();
。
采用上述 class 并将其转换为时间戳结构或 class 应该不难。
您可以像这样打印 chrono::timepoint
:
auto t0 = std::chrono::high_resolution_clock::now();
auto nanosec = t0.time_since_epoch();
std::cout << nanosec.count() << " nanoseconds since epoch\n";
std::cout << nanosec.count() / (1000000000.0 * 60.0 * 60.0) << " hours since epoch\n";
我从 How to get duration, as int milli's and float seconds from <chrono>?
那里得到了这段代码#include <chrono>
#include <iostream>
int main (int argc, char *argv[])
{
auto t0 = std::chrono::high_resolution_clock::now();
auto t1 = std::chrono::high_resolution_clock::now();
std::chrono::duration< double > fs = t1 - t0;
std::chrono::milliseconds d = std::chrono::duration_cast< std::chrono::milliseconds >( fs );
std::cout << fs.count() << "s\n";
std::cout << d.count() << "ms\n";
}
效果很好,但我如何创建时间戳:
hour:minute:second:millisecond:microsecond:nanosecond
使用 auto t0 = std::chrono::high_resolution_clock::now()
值?
我试图打印 auto t0 = std::chrono::high_resolution_clock::now();
值,看看里面有什么,但它只给我一个很大的错误堆栈:
#include <chrono>
#include <iostream>
int main (int argc, char *argv[])
{
auto t0 = std::chrono::high_resolution_clock::now();
std::cout << t0 << "\n";
}
错误:
main2.cpp: In function 'int main(int, char**)':
main2.cpp:10:13: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long long int, std::ratio<1, 1000000000> > >')
std::cout << t0 << "\n";
~~~~~~~~~~^~~~~
感谢@Miles Budnek comment, I cannot use high_resolution_clock
because it does not measure time, but CPU ticks. So, I found this answer based on Print current system time in nanoseconds using c++ chrono做到最好。
#include <chrono>
#include <ctime>
#include <time.h>
#include <iostream>
// C++ -> Numerics library -> Compile time rational arithmetic -> std::ratio
// http://en.cppreference.com/w/cpp/numeric/ratio/ratio
//
// How to convert std::chrono::high_resolution_clock::now() to milliseconds, microseconds, ...?
//
int main (int argc, char *argv[])
{
std::chrono::time_point< std::chrono::system_clock > now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();
/* UTC: -3:00 = 24 - 3 = 21 */
typedef std::chrono::duration< int, std::ratio_multiply< std::chrono::hours::period, std::ratio< 21 > >::type > Days;
Days days = std::chrono::duration_cast< Days >( duration );
duration -= days;
auto hours = std::chrono::duration_cast< std::chrono::hours >( duration );
duration -= hours;
auto minutes = std::chrono::duration_cast< std::chrono::minutes >( duration );
duration -= minutes;
auto seconds = std::chrono::duration_cast< std::chrono::seconds >( duration );
duration -= seconds;
auto milliseconds = std::chrono::duration_cast< std::chrono::milliseconds >( duration );
duration -= milliseconds;
auto microseconds = std::chrono::duration_cast< std::chrono::microseconds >( duration );
duration -= microseconds;
auto nanoseconds = std::chrono::duration_cast< std::chrono::nanoseconds >( duration );
// C library function - localtime()
// https://www.tutorialspoint.com/c_standard_library/c_function_localtime.htm
//
// struct tm {
// int tm_sec; // seconds, range 0 to 59
// int tm_min; // minutes, range 0 to 59
// int tm_hour; // hours, range 0 to 23
// int tm_mday; // day of the month, range 1 to 31
// int tm_mon; // month, range 0 to 11
// int tm_year; // The number of years since 1900
// int tm_wday; // day of the week, range 0 to 6
// int tm_yday; // day in the year, range 0 to 365
// int tm_isdst; // daylight saving time
// };
time_t theTime = time(NULL);
struct tm *aTime = localtime(&theTime);
std::cout << days.count() << " days since epoch or "
<< days.count() / 365.2524 << " years since epoch. The time is now "
<< aTime->tm_hour << ":"
<< minutes.count() << ":"
<< seconds.count() << ":"
<< milliseconds.count() << ":"
<< microseconds.count() << ":"
<< nanoseconds.count() << std::endl;
}
运行 它,输出:
$ g++ -O0 -g -Wall -std=c++11 -o test timestamp_example.cpp && ./test
20107 days since epoch or 55.0496 years since epoch. The time is now 21:39:51:935:732:700
我有一个 class,用于使用 std::chrono
进行基准计时计算。它是一个接受决议的 class 模板。此处显示:
#ifndef EXECUTION_TIMER_H
#define EXECUTION_TIMER_H
#include <chrono>
#include <type_traits>
#include <sstream>
#include <iostream>
template <class Resolution = std::chrono::milliseconds>
class ExecutionTimer {
public:
using Clock = std::conditional_t<std::chrono::high_resolution_clock::is_steady,
std::chrono::high_resolution_clock,
std::chrono::steady_clock>;
private:
const Clock::time_point mStart = Clock::now();
public:
ExecutionTimer() = default;
~ExecutionTimer() {
const auto end = Clock::now();
std::ostringstream strStream;
strStream << "Destructor Elapsed: "
<< std::chrono::duration_cast<Resolution>(end - mStart).count()
<< std::endl;
std::cout << strStream.str() << std::endl;
}
inline void stop() {
const auto end = Clock::now();
std::ostringstream strStream;
strStream << "Stop Elapsed: "
<< std::chrono::duration_cast<Resolution>(end - mStart).count()
<< std::endl;
std::cout << strStream.str() << std::endl;
}
};
#endif // !EXECUTION_TIMER_H
使用 class 非常简单,因为驱动程序将演示。
#include <vector>
#include <iostream>
#include <conio.h> // for _getch().
#include "ExecutionTimer.h"
void someFunc() {
// Some Operations Just To Get Difference In Time
std::vector<int> values;
for ( unsigned int i = 0; i < 1000; i++ ) {
values.push_back( i * 2 );
}
int printed = 0;
for ( auto i : values ) {
if ( printed % 10 == 0 ) {
std::cout << "\n";
std::cout << i << ", ";
} else {
std::cout << i << ", ";
}
printed++;
}
std::cout << std::endl;
}
int main() {
ExecutionTimer<> timer; // Default = std::chrono::miliseconds
someFunc();
timer.stop();
ExecutionTimer<std::chrono::microseconds> timer2;
someFunc();
timer2.stop();
_getch();
return 0;
}
上面的 class 正在以某种方式做我认为是你所要求的。
现在,在查看上面的代码时,尤其是此处的这些行:
std::chrono::duration< double > fs = t1 - t0; std::chrono::milliseconds d = std::chrono::duration_cast< std::chrono::milliseconds >( fs );
我相信您在将值传递给 duration_cast<>
后遗漏了 .count();
。
采用上述 class 并将其转换为时间戳结构或 class 应该不难。
您可以像这样打印 chrono::timepoint
:
auto t0 = std::chrono::high_resolution_clock::now();
auto nanosec = t0.time_since_epoch();
std::cout << nanosec.count() << " nanoseconds since epoch\n";
std::cout << nanosec.count() / (1000000000.0 * 60.0 * 60.0) << " hours since epoch\n";