{"id":682,"date":"2012-10-26T15:39:49","date_gmt":"2012-10-26T06:39:49","guid":{"rendered":"http:\/\/soundengine.jp\/wordpress\/tips\/?p=682"},"modified":"2014-02-06T23:42:25","modified_gmt":"2014-02-06T14:42:25","slug":"rdtscread-time-stamp-counter%e3%81%ae%e5%b9%b3%e5%9d%87%e5%80%a4%e6%b8%ac%e5%ae%9a","status":"publish","type":"post","link":"https:\/\/soundengine.jp\/wordpress\/tips\/try\/682\/","title":{"rendered":"rdtsc(Read Time Stamp Counter)\u306e\u5e73\u5747\u5024\u6e2c\u5b9a"},"content":{"rendered":"<p><a href=\"http:\/\/soundengine.jp\/wordpress\/tips\/2012\/07\/17\/rdtscread-time-stamp-counter%e3%81%ae%e3%82%af%e3%83%a9%e3%82%b9%e5%8c%96\/\">rdtsc(Read Time Stamp Counter)\u306e\u30af\u30e9\u30b9\u5316<\/a>\u3067\u3001rdtsc\u306e\u53d6\u5f97\u65b9\u6cd5\u3092\u8aac\u660e\u3057\u305f\u3002\u4eca\u56de\u306f\u4f55\u5ea6\u3082\u540c\u3058\u30eb\u30fc\u30c1\u30f3\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6e2c\u5b9a\u3059\u308b\u4e0a\u3067\u5e73\u5747\u7684\u306ardtsc\u3092\u6e2c\u5b9a\u3057\u3066\u307f\u308b\u3002<\/p>\n<p><!--more--><\/p>\n<h2 id=\"content_1_0\">\u6e2c\u5b9a  <a class=\"anchor_super\" id=\"i431831a\" href=\"http:\/\/soundengine.jp\/backend\/?services%2Ftips%2F2008-05-18#i431831a\" title=\"i431831a\"> <\/a><\/h2>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3092\u4f5c\u3063\u3066\u307f\u305f\u3002\u4eca\u56de\u3001\u7279\u306b\u3053\u306e\u30af\u30e9\u30b9\u3092\u4f7f\u3046\u4e0a\u3067\u306e\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u306f\u3042\u307e\u308a\u8003\u3048\u3066\u306a\u3044(\u3068\u3044\u3046\u3088\u308a\u8003\u3048\u306a\u304f\u3066\u3088\u3044\u3050\u3089\u3044\u91cd\u3044\u51e6\u7406\u3092\u6e2c\u5b9a\u3059\u308b\u5834\u5408\u306b\u4f7f\u3046\u3053\u3068\u3092\u524d\u63d0\u306b)\u3002\u307e\u305f\u3001include\u306bATL\/WTL\u7cfb\u306e\u30d8\u30c3\u30c0\u30fc\u30d5\u30a1\u30a4\u30eb\u3092\u4f7f\u3063\u3066\u3044\u308b\u304c\u3001\u3053\u308c\u306fCAtlArray\u3068CString\u3092\u4f7f\u3046\u305f\u3081\u3067\u3042\u308b\u3002vector\u3084string\u304c\u597d\u304d\u306a\u4eba\u306f\u305d\u3061\u3089\u3092\u4f7f\u3063\u3066\u3082\u3089\u3048\u308c\u3070\u3088\u3044\u304b\u3068\u3002<\/p>\n<pre>#include &lt;atlbase.h&gt;\r\n#include &lt;atlapp.h&gt;\r\n#include &lt;atlcoll.h&gt;\r\n#include &lt;atlmisc.h&gt;\r\n\r\nclass MeanRdTsc\r\n{\r\nprivate:\r\n\r\n\tstatic CAtlArray &lt; long long &gt; rdTsc_;\r\n\tstatic int size_;\r\n\r\n\tlong long oldRdTsc_;\r\n\tunsigned int count_;\r\n\r\n\tinline static long long getRdTsc()\r\n\t{  \r\n#ifdef _M_IX86\r\n\t\t_asm\r\n\t\t{\r\n\t\t\trdtsc\r\n\t\t}\r\n#endif\r\n#ifdef _M_X64\r\n\t\treturn __rdtsc();\r\n#endif\r\n\t};\r\n\r\npublic:\r\n\r\n\tMeanRdTsc() :\r\n\t\toldRdTsc_( getRdTsc() ),\r\n\t\tcount_( 0 )\r\n\t{\r\n\t};\r\n\r\n\t~MeanRdTsc()\r\n\t{\r\n\t\tsize_++;\r\n\t};\r\n\t\r\n\tvoid update()\r\n\t{\t\r\n\t\tconst long long newRdTsc = getRdTsc();\r\n\r\n\t\tif( rdTsc_.GetCount() &lt;= count_ )\r\n\t\t{\r\n\t\t\tif( !rdTsc_.SetCount( count_ + 1 ) )\r\n\t\t\t{\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\trdTsc_[ count_ ] += newRdTsc - oldRdTsc_;\r\n\t\tcount_++;\r\n\r\n\t\toldRdTsc_ = getRdTsc();\r\n\t};\r\n};\r\n\r\n__declspec( selectany ) int MeanRdTsc::size_ = 0;\r\n__declspec( selectany ) CAtlArray &lt; long long &gt; MeanRdTsc::rdTsc_;<\/pre>\n<p>\u3068\u3044\u3046\u611f\u3058\u3067\u3042\u308b\u3002static\u3092\u4f7f\u3063\u3066\u3044\u308b\u304c\u3001\u3053\u308c\u306f\u4e0b\u306e\u3088\u3046\u306a\u4f7f\u3044\u65b9\u3092\u3059\u308b\u305f\u3081\u3067\u3042\u308b\u3002<\/p>\n<pre>for( int i = 0; i &lt; 100; i++ )\r\n{\r\n\tMeanRdTsc mrt;\r\n\r\n\tprintf( \"test1\" );\r\n\r\n\tmrt.update();\r\n\r\n\tprintf( \"test2\" );\r\n\r\n\tmrt.update();\r\n}<\/pre>\n<p>\u3061\u306a\u307f\u306b\u3001\u30d8\u30c3\u30c0\u30fc\u30d5\u30a1\u30a4\u30eb\u3057\u304b\u7528\u610f\u3057\u3066\u3044\u306a\u3044\u306e\u3067\u3001static\u30e1\u30f3\u30d0\u306e\u521d\u671f\u5316\u306b<a href=\"http:\/\/www.google.co.jp\/search?ie=utf8&amp;oe=utf8&amp;q=__declspec%28selectany%29&amp;lr=lang_ja&amp;hl=ja\" title=\"Google:__declspec(selectany)\" rel=\"nofollow\">__declspec(selectany)<\/a>\u3092\u4f7f\u3063\u3066\u3044\u308b\u3002<\/p>\n<div class=\"jumpmenu\">\n<\/div>\n<h2 id=\"content_1_1\">\u5e73\u5747\u306e\u51fa\u529b  <a class=\"anchor_super\" id=\"m76438be\" href=\"http:\/\/soundengine.jp\/backend\/?services%2Ftips%2F2008-05-18#m76438be\" title=\"m76438be\"> <\/a><\/h2>\n<p>\u51fa\u529b\u3068\u3057\u3066\u306f\u3053\u3093\u306a\u30e1\u30bd\u30c3\u30c9\u3092\u7528\u610f\u3059\u308c\u3070\u3088\u3044\u306e\u3067\u306f\u306a\u3044\u304b\u3068\u3002\u5e73\u5747\u3068\u3044\u3046\u3053\u3068\u3067\u56de\u6570\u3067\u5272\u3063\u3066\u3044\u308b\u3002<\/p>\n<pre>\tstatic void trace()\r\n\t{\r\n\t\tif( size_ &lt;= 0 )\r\n\t\t{\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tCString temp;\r\n\t\tCString trace;\r\n\r\n\t\tfor( int i = 0; i &lt; ( int )rdTsc_.GetCount(); i++ )\r\n\t\t{\r\n\t\t\ttemp.Format( _T( \"%I64d\" ), rdTsc_[ i ] \/ size_ );\r\n\t\t\t\r\n\t\t\tif( !trace.IsEmpty() )\r\n\t\t\t{\r\n\t\t\t\ttrace += _T( \",\" );\r\n\t\t\t}\r\n\r\n\t\t\ttrace += temp;\r\n\t\t}\r\n\r\n\t\tATLTRACE2( CT2A( trace + _T( \"\\n\" ) ) );\r\n\t};\r\n\r\n\t\/\/\/ \u30d7\u30ea\u30f3\u30c8\r\n\tstatic void print()\r\n\t{\r\n\t\tif( size_ &lt;= 0 )\r\n\t\t{\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tCString temp;\r\n\t\tCString trace;\r\n\r\n\t\tfor( int i = 0; i &lt; ( int )rdTsc_.GetCount(); i++ )\r\n\t\t{\r\n\t\t\ttemp.Format( _T( \"%I64d\" ), rdTsc_[ i ] \/ size_ );\r\n\t\t\t\r\n\t\t\tif( !trace.IsEmpty() )\r\n\t\t\t{\r\n\t\t\t\ttrace += _T( \",\" );\r\n\t\t\t}\r\n\r\n\t\t\ttrace += temp;\r\n\t\t}\r\n\r\n\t\tprintf( CT2A( trace + _T( \"\\n\" ) ) );\r\n\t};<\/pre>\n<p>\u6bce\u56de\u3001\u3082\u3057\u304f\u306f\u6700\u5f8c\u306b\u3053\u308c\u3089\u3092\u547c\u3093\u3067\u3042\u3052\u308c\u3070\u3088\u3044\u304b\u3068\u3002printf\u3084Unicode\u30d0\u30fc\u30b8\u30e7\u30f3\u306ewprintf\u304c\u3042\u308b\u304c\u3001<a href=\"http:\/\/msdn.microsoft.com\/ja-jp\/library\/wc7014hz(VS.80).aspx\" rel=\"nofollow\">MSDN<\/a>\u3067\u8abf\u3079\u305f\u3068\u3053\u308d\u3067\u306f(\u305d\u308c\u3092\u5f15\u7528\u3057\u3066)\u3001<\/p>\n<blockquote>\n<p class=\"quotation\">\u73fe\u5728 printf \u3067\u306f\u3001UNICODE \u30b9\u30c8\u30ea\u30fc\u30e0\u3078\u306e\u51fa\u529b\u306f\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002<\/p>\n<\/blockquote>\n<p>\u3068\u306e\u3053\u3068\u3067\u3001\u7d50\u5c40<a href=\"http:\/\/www.google.co.jp\/search?ie=utf8&amp;oe=utf8&amp;q=CT2A&amp;lr=lang_ja&amp;hl=ja\" title=\"Google:CT2A\" rel=\"nofollow\">CT2A<\/a>\u306a\u3069\u3067ANSI\u6587\u5b57\u5217\u306b\u5909\u63db\u3057\u666e\u901a\u306bprintf\u3092\u4f7f\u3046\u306e\u304c\u3088\u3044\u3067\u3042\u308d\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>rdtsc(Read Time Stamp Counter)\u306e\u30af\u30e9\u30b9\u5316\u3067\u3001rdt &hellip; <a href=\"https:\/\/soundengine.jp\/wordpress\/tips\/try\/682\/\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-682","post","type-post","status-publish","format-standard","hentry","category-try"],"_links":{"self":[{"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/posts\/682","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/comments?post=682"}],"version-history":[{"count":12,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/posts\/682\/revisions"}],"predecessor-version":[{"id":3216,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/posts\/682\/revisions\/3216"}],"wp:attachment":[{"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/media?parent=682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/categories?post=682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/soundengine.jp\/wordpress\/tips\/wp-json\/wp\/v2\/tags?post=682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}