参照、例えば、How to realise long-term high-resolution timing on windows using C++?とC++ Timer function to provide time in nano secondsのための誰に
感謝。
Windows XPでCygwinでいくつかのテストを行いました。私のマシンでは、gettimeofday()の粒度は約15ミリ秒(約1/64秒)です。それはかなり粗いです。 (最初のための1000000を有することができるPOSIX)
* clock_t clock(void) (divisor CLOCKS_PER_SEC)
* clock_t times(struct tms *) (divisor sysconf(_SC_CLK_TCK))
どちらの約数が1000以下のとおりですので、の粒度です。
また、clock_getres(CLOCK_REALTIME、...)は15ミリ秒を返しますので、clock_gettime()は役に立ちません。 CLOCK_MONOTONICとCLOCK_PROCESS_CPUTIME_IDは機能しません。
その他の可能性のあるWindowsの可能性がありますRDTSC;ウィキペディアの記事を参照してください。 Windows XPでは利用できないHPETがあります。
Linuxではclock()が処理時間ですが、Windowsでは壁面時間です。
だから、いくつかのサンプルコード、両方の標準的なUnixのため、および(
私のマシン上で)約50マイクロ秒の精度を与えるWindowsで実行CYGWINコード、ため。戻り値は秒単位で、関数が最初に呼び出されてから経過した秒数を示します。 (私は後天的にこれが私が
a year agoを与えた答えであることに気付いた)。
std::chrono::high_resolution_clock
:Windowsの、UNIX、Linux、および漠然と近代的なものとのポータブル
#ifndef __CYGWIN32__
double RealElapsedTime(void) { // returns 0 seconds first time called
static struct timeval t0;
struct timeval tv;
gettimeofday(&tv, 0);
if (!t0.tv_sec)
t0 = tv;
return tv.tv_sec - t0.tv_sec + (tv.tv_usec - t0.tv_usec)/1000000.;
}
#else
#include <windows.h>
double RealElapsedTime(void) { // granularity about 50 microsecs on my machine
static LARGE_INTEGER freq, start;
LARGE_INTEGER count;
if (!QueryPerformanceCounter(&count))
FatalError("QueryPerformanceCounter");
if (!freq.QuadPart) { // one time initialization
if (!QueryPerformanceFrequency(&freq))
FatalError("QueryPerformanceFrequency");
start = count;
}
return (double)(count.QuadPart - start.QuadPart)/freq.QuadPart;
}
#endif
QueryPerformanceCounterに問題がありますか? – kichik
@kichikデュアルコアCPUと一緒に使うと動作すると聞きました。完全に真であるかどうかわからない – Quillion
新しい(vs2015、v14.0.xyzにバンドルされている)ランタイムには、timespecタイプ(実際には_timespec32、_timespec64、およびtimespecの3つの異なるタイプ)を定義するヘッダーが含まれています。残念ながら、このタイプの存在をテストするための付随するマクロ(_TIMESPEC_DEFINEDなど)はありません。 –