// RDTSC の数値と実際の時間の関係をもとめる=CPUクロックの測定 // gcc -o tscpersec tscpersec.c #include #include #include inline unsigned long long int RDTSC(void) { unsigned int h,l; /* read Pentium cycle counter */ __asm__(".byte 0x0f,0x31" :"=a" (l), "=d" (h)); return ((unsigned long long int)h<<32)|l; } int main(void) { struct timeval tv1,tv2; unsigned long long tsc1,tsc2; gettimeofday(&tv2,NULL); tsc2=RDTSC(); sleep(1); gettimeofday(&tv2,NULL); tsc2=RDTSC(); while(1) { sleep(1); gettimeofday(&tv1,NULL); tsc1=RDTSC(); printf("%ld usec , %lld clocks -> %lg clocks/sec \n", (tv1.tv_sec-tv2.tv_sec)*1000000+(tv1.tv_usec-tv2.tv_usec), tsc1-tsc2, (double)(tsc1-tsc2)/ ((tv1.tv_sec-tv2.tv_sec)*1000000+(tv1.tv_usec-tv2.tv_usec))* 1e6); // 短時間の数値を求めるなら, 以下、コメントアウト // tsc2=tsc1; // tv2=tv1; } }