{"id":36,"date":"2024-11-27T15:18:06","date_gmt":"2024-11-27T07:18:06","guid":{"rendered":"https:\/\/genetop.top\/?p=36"},"modified":"2024-12-14T09:22:43","modified_gmt":"2024-12-14T01:22:43","slug":"c-stl-chrono-vs-monotonic_time_get_milliseconds","status":"publish","type":"post","link":"https:\/\/genetop.top\/index.php\/2024\/11\/27\/c-stl-chrono-vs-monotonic_time_get_milliseconds\/","title":{"rendered":"C++ STL chrono\u548cmonotonic_time_get_milliseconds\u7684\u6027\u80fd\u5bf9\u6bd4"},"content":{"rendered":"<h3><strong>1. \u6838\u5fc3\u533a\u522b<\/strong><\/h3>\n<ol>\n<li><strong><code>monotonic_time_get_milliseconds<\/code><\/strong>\uff1a\n<ul>\n<li>\u901a\u5e38\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6216\u5e73\u53f0\u76f8\u5173\u7684\u51fd\u6570\u3002<\/li>\n<li>\u5982\u679c\u5e95\u5c42\u5b9e\u73b0\u4f9d\u8d56\u4e8e <code>clock_gettime(CLOCK_MONOTONIC)<\/code>\uff08Linux\/Unix\uff09\u6216\u7c7b\u4f3c\u7684 API\uff0c\u5176\u6027\u80fd\u4f1a\u4e0e\u5e73\u53f0\u7684\u7cfb\u7edf\u8c03\u7528\u5f00\u9500\u76f4\u63a5\u76f8\u5173\u3002<\/li>\n<li>\u63d0\u4f9b\u65f6\u95f4\u6233\u901a\u5e38\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\uff0c\u53ef\u80fd\u4f1a\u6d89\u53ca\u989d\u5916\u7684\u8f6c\u6362\uff08\u5982\u4ece\u7eb3\u79d2\u5230\u6beb\u79d2\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong><code>std::chrono::steady_clock::now()<\/code><\/strong>\uff1a\n<ul>\n<li>C++ \u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u57fa\u4e8e\u5e73\u53f0\u7684\u5355\u8c03\u65f6\u949f\u5b9e\u73b0\u3002<\/li>\n<li>\u5728\u5927\u591a\u6570\u5e73\u53f0\u4e0a\uff0c<code>std::chrono::steady_clock<\/code> \u901a\u5e38\u5305\u88c5\u4e86\u5e95\u5c42\u7684\u5355\u8c03\u65f6\u949f API\uff1a\n<ul>\n<li>Linux\/Unix\uff1a<code>clock_gettime(CLOCK_MONOTONIC)<\/code><\/li>\n<li>Windows\uff1a<code>QueryPerformanceCounter<\/code><\/li>\n<li>macOS\uff1a<code>mach_absolute_time<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\u63d0\u4f9b\u7684\u65f6\u95f4\u5355\u4f4d\u662f <code>std::chrono::time_point<\/code>\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u9700\u8981\u7684\u65f6\u95f4\u7cbe\u5ea6\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<hr \/>\n<h3><strong>2. \u6027\u80fd\u6bd4\u8f83<\/strong><\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e24\u8005\u6027\u80fd\u7684\u4e3b\u8981\u6bd4\u8f83\u70b9\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th><strong>\u6307\u6807<\/strong><\/th>\n<th><strong><code>monotonic_time_get_milliseconds<\/code><\/strong><\/th>\n<th><strong><code>std::chrono::steady_clock::now()<\/code><\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>\u8c03\u7528\u5f00\u9500<\/strong><\/td>\n<td>\u4f9d\u8d56\u4e8e\u5b9e\u73b0\uff0c\u901a\u5e38\u63a5\u8fd1\u5e95\u5c42API\u6027\u80fd<\/td>\n<td>\u5305\u88c5\u5e95\u5c42API\uff0c\u989d\u5916\u5f00\u9500\u6781\u5c0f<\/td>\n<\/tr>\n<tr>\n<td><strong>\u65f6\u95f4\u7cbe\u5ea6<\/strong><\/td>\n<td>\u6beb\u79d2\uff08\u53ef\u80fd\u56fa\u5b9a\uff09<\/td>\n<td>\u53ef\u63d0\u4f9b\u7eb3\u79d2\u6216\u5176\u4ed6\u7cbe\u5ea6<\/td>\n<\/tr>\n<tr>\n<td><strong>\u53ef\u79fb\u690d\u6027<\/strong><\/td>\n<td>\u53ef\u80fd\u53d7\u9650\u4e8e\u5b9e\u73b0<\/td>\n<td>\u9ad8\uff0c\u8de8\u5e73\u53f0\u4e00\u81f4<\/td>\n<\/tr>\n<tr>\n<td><strong>\u989d\u5916\u8f6c\u6362\u5f00\u9500<\/strong><\/td>\n<td>\u53ef\u80fd\u9700\u4ece\u7eb3\u79d2\u5230\u6beb\u79d2\u8f6c\u6362<\/td>\n<td>\u65e0\u9700\u624b\u52a8\u8f6c\u6362\uff0c\u6309\u9700\u9009\u62e9\u5355\u4f4d<\/td>\n<\/tr>\n<tr>\n<td><strong>\u5178\u578b\u6027\u80fd\uff08Linux\uff09<\/strong><\/td>\n<td>20-60 \u7eb3\u79d2\uff08\u5982\u679c\u57fa\u4e8e <code>clock_gettime<\/code>\uff09<\/td>\n<td>50-100 \u7eb3\u79d2\uff08\u8f7b\u5fae\u989d\u5916\u5f00\u9500\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3><strong>3. \u5177\u4f53\u6bd4\u8f83\u573a\u666f<\/strong><\/h3>\n<ol>\n<li><strong>\u4f4e\u7ea7\u6027\u80fd\u6bd4\u8f83<\/strong>\uff1a\n<ul>\n<li>\u5982\u679c <code>monotonic_time_get_milliseconds<\/code> \u662f\u76f4\u63a5\u5c01\u88c5\u4e86 <code>clock_gettime(CLOCK_MONOTONIC)<\/code> \u6216\u7c7b\u4f3c\u7684\u51fd\u6570\uff0c\u5176\u6027\u80fd\u53ef\u80fd\u7a0d\u5fae\u4f18\u4e8e <code>std::chrono::steady_clock::now()<\/code>\uff0c\u56e0\u4e3a\u540e\u8005\u4f1a\u7ecf\u8fc7\u8f7b\u91cf\u7684\u6807\u51c6\u5e93\u5c01\u88c5\u3002<\/li>\n<li>\u5bf9\u4e8e\u9ad8\u9891\u8c03\u7528\u573a\u666f\uff08\u5982\u6e38\u620f\u5f15\u64ce\u6216\u5b9e\u65f6\u7cfb\u7edf\uff09\uff0c\u8fd9\u79cd\u5fae\u5c0f\u7684\u5f00\u9500\u53ef\u80fd\u4f1a\u7d2f\u79ef\uff0c\u4f46\u5dee\u5f02\u4ecd\u7136\u975e\u5e38\u5c0f\uff08\u4e00\u822c\u4e0d\u8d85\u8fc7\u51e0\u5341\u7eb3\u79d2\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u7075\u6d3b\u6027\u4e0e\u53ef\u7528\u6027<\/strong>\uff1a\n<ul>\n<li><code>std::chrono::steady_clock<\/code> \u63d0\u4f9b\u4e86\u66f4\u591a\u7075\u6d3b\u6027\uff0c\u652f\u6301\u591a\u79cd\u65f6\u95f4\u7cbe\u5ea6\uff08\u7eb3\u79d2\u3001\u5fae\u79d2\u3001\u6beb\u79d2\u7b49\uff09\uff0c\u540c\u65f6\u662f\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5177\u5907\u66f4\u9ad8\u7684\u53ef\u79fb\u690d\u6027\u3002<\/li>\n<li>\u5982\u679c\u53ea\u9700\u8981\u6beb\u79d2\u7cbe\u5ea6\uff0c\u5e76\u4e14\u6027\u80fd\u81f3\u5173\u91cd\u8981\uff0c<code>monotonic_time_get_milliseconds<\/code> \u7684\u7b80\u5355\u5b9e\u73b0\u53ef\u80fd\u4f1a\u66f4\u5feb\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u4ee3\u7801\u7ef4\u62a4\u4e0e\u53ef\u9760\u6027<\/strong>\uff1a\n<ul>\n<li>\u4f7f\u7528 <code>std::chrono::steady_clock<\/code> \u53ef\u4ee5\u51cf\u5c11\u5bf9\u7279\u5b9a\u5e73\u53f0 API \u7684\u4f9d\u8d56\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u8de8\u5e73\u53f0\u6027\u3002<\/li>\n<li>\u81ea\u5b9a\u4e49\u7684 <code>monotonic_time_get_milliseconds<\/code> \u53ef\u80fd\u9700\u8981\u989d\u5916\u8003\u8651\u5e73\u53f0\u517c\u5bb9\u6027\u548c\u6f5c\u5728\u7684\u9519\u8bef\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<hr \/>\n<h3><strong>4. \u5b9e\u9645\u6027\u80fd\u6d4b\u8bd5<\/strong><\/h3>\n<p>\u5728\u5b9e\u8df5\u4e2d\uff0c\u6027\u80fd\u5dee\u5f02\u53ef\u4ee5\u901a\u8fc7\u5fae\u57fa\u51c6\u6d4b\u8bd5\u6765\u8bc4\u4f30\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684 C++ \u6027\u80fd\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code><code>#include<br \/>\n#include<br \/>\n#include<\/code><\/code>\/\/ Example monotonic_time_get_milliseconds implementation<br \/>\nlong long monotonic_time_get_milliseconds() {<br \/>\nstruct timespec ts;<br \/>\nclock_gettime(CLOCK_MONOTONIC, &amp;ts);<br \/>\nreturn ts.tv_sec * 1000LL + ts.tv_nsec \/ 1000000LL;<br \/>\n}<\/p>\n<p><code><code><\/code><\/code>int main() {<br \/>\nconstexpr int iterations = 1000000;<\/p>\n<p><code><code><\/code><\/code>\/\/ Test monotonic_time_get_milliseconds<br \/>\nauto start = std::chrono::high_resolution_clock::now();<br \/>\nfor (int i = 0; i &lt; iterations; ++i) {<br \/>\nvolatile auto time = monotonic_time_get_milliseconds();<br \/>\n}<br \/>\nauto end = std::chrono::high_resolution_clock::now();<br \/>\nstd::cout &lt;&lt; &#8220;monotonic_time_get_milliseconds: &#8221;<br \/>\n&lt;&lt; std::chrono::duration_cast(end &#8211; start).count()<br \/>\n&lt;&lt; &#8221; microseconds\\n&#8221;;<\/p>\n<p><code><code><\/code><\/code>\/\/ Test std::chrono::steady_clock::now<br \/>\nstart = std::chrono::high_resolution_clock::now();<br \/>\nfor (int i = 0; i &lt; iterations; ++i) {<br \/>\nvolatile auto time = std::chrono::steady_clock::now();<br \/>\n}<br \/>\nend = std::chrono::high_resolution_clock::now();<br \/>\nstd::cout &lt;&lt; &#8220;std::chrono::steady_clock::now: &#8221;<br \/>\n&lt;&lt; std::chrono::duration_cast(end &#8211; start).count()<br \/>\n&lt;&lt; &#8221; microseconds\\n&#8221;;<\/p>\n<p><code><code><\/code><\/code>return 0;<br \/>\n}<\/p>\n<p>\u8f93\u51fa\uff1a<br \/>\nmonotonic_time_get_milliseconds: 21185 microseconds<br \/>\nstd::chrono::steady_clock::now: 21424 microseconds<\/p>\n<\/div>\n<\/div>\n<hr \/>\n<h3><strong>5. \u603b\u7ed3<\/strong><\/h3>\n<ul>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\n<ul>\n<li>\u5982\u679c <code>monotonic_time_get_milliseconds<\/code> \u662f\u76f4\u63a5\u8c03\u7528\u5e95\u5c42 API\uff0c\u5b83\u7684\u6027\u80fd\u53ef\u80fd\u7565\u9ad8\uff0c\u4f46\u5dee\u8ddd\u901a\u5e38\u5f88\u5c0f\u3002<\/li>\n<li>\u5bf9\u4e8e\u7edd\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\uff0c<code>std::chrono::steady_clock::now()<\/code> \u7684\u989d\u5916\u5f00\u9500\u53ef\u4ee5\u5ffd\u7565\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u63a8\u8350<\/strong>\uff1a\n<ul>\n<li>\u5982\u679c\u9700\u8981\u9ad8\u6027\u80fd\u8ba1\u65f6\uff0c\u5e76\u4e14\u4ee3\u7801\u9700\u8de8\u5e73\u53f0\uff0c\u4f7f\u7528 <code>std::chrono::steady_clock::now()<\/code> \u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/li>\n<li>\u5982\u679c\u53ea\u5173\u6ce8\u6beb\u79d2\u7ea7\u522b\u6027\u80fd\uff0c\u4e14\u4ee3\u7801\u9650\u5b9a\u5728\u7279\u5b9a\u5e73\u53f0\u4e0a\uff0c<code>monotonic_time_get_milliseconds<\/code> \u53ef\u80fd\u4f1a\u6709\u5fae\u5c0f\u4f18\u52bf\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. \u6838\u5fc3\u533a\u522b monotonic_time_get_milliseconds\uff1a \u901a\u5e38\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6216\u5e73\u53f0\u76f8\u5173\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-c","category-language"],"_links":{"self":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":5,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":39,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/posts\/36\/revisions\/39"}],"wp:attachment":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}