{"id":287,"date":"2013-05-30T19:05:40","date_gmt":"2013-05-30T11:05:40","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=287"},"modified":"2013-05-30T19:05:40","modified_gmt":"2013-05-30T11:05:40","slug":"c-%e7%b2%be%e7%a1%ae%e5%88%b0%e5%be%ae%e7%a7%92%e8%ae%a1%e6%97%b6","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=287","title":{"rendered":"C++ \u7cbe\u786e\u5230\u7eb3\u79d2\u8ba1\u65f6"},"content":{"rendered":"<pre>\n#include <iostream>\nusing namespace std;\ninline unsigned __int64 GetCycleCount()\n{\n\t__asm _emit 0x0f\n\t__asm _emit 0x31\n}\nint main()\n{\n\tunsigned long t0, t1;\n\tt0 = GetCycleCount();\n\tt1 = GetCycleCount();\n\tcout << t1 - t0 << endl;\n\treturn 0;\n}\n<\/pre>\n<p>\u53c2\u8003\u6587\u732e\uff1ahttp:\/\/yanlinjust79.blog.163.com\/blog\/static\/16950848220107254332975\/<br \/>\n<quote><br \/>\n\u5728Intel Pentium\u4ee5\u4e0a\u7ea7\u522b\u7684CPU\u4e2d\uff0c\u6709\u4e00\u4e2a\u79f0\u4e3a\u201c\u65f6\u95f4\u6233\uff08Time Stamp\uff09\u201d\u7684\u90e8\u4ef6\uff0c\u5b83\u4ee564\u4f4d\u65e0\u7b26\u53f7\u6574\u578b\u6570\u7684\u683c\u5f0f\uff0c\u8bb0\u5f55\u4e86\u81eaCPU\u4e0a\u7535\u4ee5\u6765\u6240\u7ecf\u8fc7\u7684\u65f6\u949f\u5468\u671f\u6570\u3002\u7531\u4e8e\u76ee\u524d\u7684CPU\u4e3b\u9891\u90fd\u975e\u5e38\u9ad8\uff0c\u56e0\u6b64\u8fd9\u4e2a\u90e8\u4ef6\u53ef\u4ee5\u8fbe\u5230\u7eb3\u79d2\u7ea7\u7684\u8ba1\u65f6\u7cbe\u5ea6\u3002\u8fd9\u4e2a\u7cbe\u786e\u6027\u662f\u4e0a\u8ff0\u4e24\u79cd\u65b9\u6cd5\u6240\u65e0\u6cd5\u6bd4\u62df\u7684\u3002<br \/>\n\u5728Pentium\u4ee5\u4e0a\u7684CPU\u4e2d\uff0c\u63d0\u4f9b\u4e86\u4e00\u6761\u673a\u5668\u6307\u4ee4RDTSC\uff08Read Time Stamp Counter\uff09\u6765\u8bfb\u53d6\u8fd9\u4e2a\u65f6\u95f4\u6233\u7684\u6570\u5b57\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5728EDX:EAX\u5bc4\u5b58\u5668\u5bf9\u4e2d\u3002\u7531\u4e8eEDX:EAX\u5bc4\u5b58\u5668\u5bf9\u6070\u597d\u662fWin32\u5e73\u53f0\u4e0bC++\u8bed\u8a00\u4fdd\u5b58\u51fd\u6570\u8fd4\u56de\u503c\u7684\u5bc4\u5b58\u5668\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u628a\u8fd9\u6761\u6307\u4ee4\u770b\u6210\u662f\u4e00\u4e2a\u666e\u901a\u7684\u51fd\u6570\u8c03\u7528\u3002<br \/>\n\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\uff1a<br \/>\n1.\u9ad8\u7cbe\u5ea6\u3002\u53ef\u4ee5\u76f4\u63a5\u8fbe\u5230\u7eb3\u79d2\u7ea7\u7684\u8ba1\u65f6\u7cbe\u5ea6\uff08\u57281GHz\u7684CPU\u4e0a\u6bcf\u4e2a\u65f6\u949f\u5468\u671f\u5c31\u662f\u4e00\u7eb3\u79d2\uff09\uff0c\u8fd9\u662f\u5176\u4ed6\u8ba1\u65f6\u65b9\u6cd5\u6240\u96be\u4ee5\u4f01\u53ca\u7684\u3002<br \/>\n2.\u6210\u672c\u4f4e\u3002timeGetTime \u51fd\u6570\u9700\u8981\u94fe\u63a5\u591a\u5a92\u4f53\u5e93winmm.lib\uff0cQueryPerformance* \u51fd\u6570\u6839\u636eMSDN\u7684\u8bf4\u660e\uff0c\u9700\u8981\u786c\u4ef6\u7684\u652f\u6301\uff08\u867d\u7136\u6211\u8fd8\u6ca1\u6709\u89c1\u8fc7\u4e0d\u652f\u6301\u7684\u673a\u5668\uff09\u548cKERNEL\u5e93\u7684\u652f\u6301\uff0c\u6240\u4ee5\u4e8c\u8005\u90fd\u53ea\u80fd\u5728Windows\u5e73\u53f0\u4e0b\u4f7f\u7528\uff08\u5173\u4e8eDOS\u5e73\u53f0\u4e0b\u7684\u9ad8\u7cbe\u5ea6\u8ba1\u65f6\u95ee\u9898\uff0c\u53ef\u4ee5\u53c2\u8003\u300a\u56fe\u5f62\u7a0b\u5e8f\u5f00\u53d1\u4eba\u5458\u6307\u5357\u300b\uff0c\u91cc\u9762\u6709\u5173\u4e8e\u63a7\u5236\u5b9a\u65f6\u56688253\u7684\u8be6\u7ec6\u8bf4\u660e\uff09\u3002\u4f46RDTSC\u6307\u4ee4\u662f\u4e00\u6761CPU\u6307\u4ee4\uff0c\u51e1\u662fi386\u5e73\u53f0\u4e0bPentium\u4ee5\u4e0a\u7684\u673a\u5668\u5747\u652f\u6301\uff0c\u751a\u81f3\u6ca1\u6709\u5e73\u53f0\u7684\u9650\u5236\uff08\u6211\u76f8\u4fe1i386\u7248\u672cUNIX\u548cLinux\u4e0b\u8fd9\u4e2a\u65b9\u6cd5\u540c\u6837\u9002\u7528\uff0c\u4f46\u6ca1\u6709\u6761\u4ef6\u8bd5\u9a8c\uff09\uff0c\u800c\u4e14\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\u662f\u6700\u5c0f\u7684\u3002<br \/>\n3.\u5177\u6709\u548cCPU\u4e3b\u9891\u76f4\u63a5\u5bf9\u5e94\u7684\u901f\u7387\u5173\u7cfb\u3002\u4e00\u4e2a\u8ba1\u6570\u76f8\u5f53\u4e8e1\/(CPU\u4e3b\u9891Hz\u6570)\u79d2\uff0c\u8fd9\u6837\u53ea\u8981\u77e5\u9053\u4e86CPU\u7684\u4e3b\u9891\uff0c\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u51fa\u65f6\u95f4\u3002\u8fd9\u548cQueryPerformanceCount\u4e0d\u540c\uff0c\u540e\u8005\u9700\u8981\u901a\u8fc7QueryPerformanceFrequency\u83b7\u53d6\u5f53\u524d\u8ba1\u6570\u5668\u6bcf\u79d2\u7684\u8ba1\u6570\u6b21\u6570\u624d\u80fd\u6362\u7b97\u6210\u65f6\u95f4\u3002<br \/>\n\u8fd9\u4e2a\u65b9\u6cd5\u7684\u7f3a\u70b9\u662f\uff1a<br \/>\n1.\u73b0\u6709\u7684C\/C++\u7f16\u8bd1\u5668\u591a\u6570\u4e0d\u76f4\u63a5\u652f\u6301\u4f7f\u7528RDTSC\u6307\u4ee4\uff0c\u9700\u8981\u7528\u76f4\u63a5\u5d4c\u5165\u673a\u5668\u7801\u7684\u65b9\u5f0f\u7f16\u7a0b\uff0c\u6bd4\u8f83\u9ebb\u70e6\u3002<br \/>\n2.\u6570\u636e\u6296\u52a8\u6bd4\u8f83\u5389\u5bb3\u3002\u5176\u5b9e\u5bf9\u4efb\u4f55\u8ba1\u91cf\u624b\u6bb5\u800c\u8a00\uff0c\u7cbe\u5ea6\u548c\u7a33\u5b9a\u6027\u6c38\u8fdc\u662f\u4e00\u5bf9\u77db\u76fe\u3002\u5982\u679c\u7528\u4f4e\u7cbe\u5ea6\u7684timeGetTime\u6765\u8ba1\u65f6\uff0c\u57fa\u672c\u4e0a\u6bcf\u6b21\u8ba1\u65f6\u7684\u7ed3\u679c\u90fd\u662f\u76f8\u540c\u7684\uff1b\u800cRDTSC\u6307\u4ee4\u6bcf\u6b21\u7ed3\u679c\u90fd\u4e0d\u4e00\u6837\uff0c\u7ecf\u5e38\u6709\u51e0\u767e\u751a\u81f3\u4e0a\u5343\u7684\u5dee\u8ddd\u3002\u8fd9\u662f\u8fd9\u79cd\u65b9\u6cd5\u9ad8\u7cbe\u5ea6\u672c\u8eab\u56fa\u6709\u7684\u77db\u76fe\u3002<br \/>\n<\/quote><\/p>\n","protected":false},"excerpt":{"rendered":"<p>#include using namespace std; inline unsigned __int64 GetCycleCount() { __asm _emit 0x0f __asm _emit 0x31 } int main() { unsigned long t0, t1; t0 = GetCycleCount(); t1 = GetCycleCount(); cout<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[28],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-study","tag-c"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=287"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}