一、 获取代码运行时间
#include <iostream> #include <chrono> auto start_time = std::chrono::high_resolution_clock::now(); ... auto end_time = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time); std::cout <<"running time is : " << duration.count() << "ms" << std::endl;
二、 使用函数获取当前时间戳
#include <ctime> #include <string> #include <chrono> #include <sstream> #include <iostream> std::string getcurrenttimestamp(int time_stamp_type = 0) { std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); std::time_t now_time_t = std::chrono::system_clock::to_time_t(now); std::tm* now_tm = std::localtime(&now_time_t); char buffer[128]; strftime(buffer, sizeof(buffer), "%f %t", now_tm); std::ostringstream ss; ss.fill('0'); std::chrono::milliseconds ms; std::chrono::microseconds cs; std::chrono::nanoseconds ns; switch (time_stamp_type) { case 0: ss << buffer; break; case 1: ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000; ss << buffer << ":" << ms.count(); break; case 2: ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000; cs = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()) % 1000000; ss << buffer << ":" << ms.count() << ":" << cs.count() % 1000; break; case 3: ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000; cs = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()) % 1000000; ns = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()) % 1000000000; ss << buffer << ":" << ms.count() << ":" << cs.count() % 1000 << ":" << ns.count() % 1000; break; default: ss << buffer; break; } return ss.str(); } int main() { std::cout << getcurrenttimestamp(0) << std::endl; std::cout << getcurrenttimestamp(1) << std::endl; std::cout << getcurrenttimestamp(2) << std::endl; std::cout << getcurrenttimestamp(3) << std::endl; return 0; }
结果输出:
2022-05-27 14:35:58 2022-05-27 14:35:58:879 2022-05-27 14:35:58:879:200 2022-05-27 14:35:58:879:200:100
三、 获取时间戳的总结
1. 使用gettimeofday
函数获取毫秒级时间戳
gettimeofday
是linux系统提供的一个函数,可以获取当前的时间(精确到微秒)。通过这个函数,我们可以轻松获取毫秒级的时间戳,并将其格式化为人类可读的日期时间字符串。
#include <sys/time.h> #include <ctime> #include <string> #include <algorithm> static std::string getcurrenttime() { struct timeval tv; gettimeofday(&tv, null); static const int max_buffer_size = 128; char timestamp_str[max_buffer_size]; time_t sec = static_cast<time_t>(tv.tv_sec); int ms = static_cast<int>(tv.tv_usec) / 1000; struct tm tm_time; localtime_r(&sec, &tm_time); static const char *formater = "%4d-%02d-%02d %02d:%02d:%02d.%03d"; int wsize = snprintf(timestamp_str, max_buffer_size, formater, tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ms); timestamp_str[std::min(wsize, max_buffer_size - 1)] = '\0'; return std::string(timestamp_str); }
优点:
- 精确到毫秒。
- 简单易用,代码直观。
缺点:
- 是linux特有的函数,跨平台性较差。
- 随着时间的发展,一些系统可能推荐使用更现代的c++标准库函数。
2. 使用标准库chrono获取毫秒级时间戳
c++11引入了<chrono>
库,提供了更加现代和跨平台的时间处理功能。通过std::chrono
,我们可以轻松获取当前时间,并将其转换为毫秒级时间戳。
#include <chrono> #include <ctime> #include <string> #include <iomanip> #include <sstream> static std::string getcurrenttime() { auto now = std::chrono::system_clock::now(); auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()); auto sectime = std::chrono::duration_cast<std::chrono::seconds>(now_ms); int32_t milliseconds = now_ms.count() % 1000; std::time_t timet = sectime.count(); struct tm curtime; localtime_r(&timet, &curtime); char buffer[64]; sprintf(buffer, "%4d-%02d-%02d %02d:%02d:%02d.%03d", curtime.tm_year + 1900, curtime.tm_mon + 1, curtime.tm_mday, curtime.tm_hour, curtime.tm_min, curtime.tm_sec, milliseconds); return std::string(buffer); }
优点:
- 跨平台性好,适用于所有支持c++11及以上标准的编译器。
- 提供了丰富的时间处理功能,如时间点的加减、持续时间的转换等。
缺点:
- 相比
gettimeofday
,代码稍微复杂一些。
3. 使用std::ctime获取秒级时间戳
std::ctime
是c++标准库中的一个函数,可以将std::time_t
类型的时间转换为人类可读的日期时间字符串。虽然它只能精确到秒,但在某些不需要毫秒级精度的场景中仍然非常有用。
#include <ctime> #include <string> static std::string getcurrenttime0() { std::time_t result = std::time(nullptr); std::string ret; ret.resize(64); int wsize = sprintf((char *)&ret[0], "%s", std::ctime(&result)); ret.resize(wsize - 1); // 去除末尾的换行符 return ret; }
优点:
- 代码简单,易于理解。
- 适用于不需要毫秒级精度的场景。
缺点:
- 精度只能到秒。
- 输出的字符串包含换行符,需要手动去除。
到此这篇关于c++获取当前时间戳的几种常用方法的文章就介绍到这了,更多相关c++获取当前时间戳内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论