
Cycle-exact Pentium timing routine for MSVC 4.2
// cycle-excact timing routine for the Pentium under Windows 95 and NT
#include <windows.h>
#include <stdio.h>
#define _TimerCount 1000
#define rdtsc __asm __emit(0x0F) __asm __emit(0x31)
#define B __asm { \
__asm push EAX \
__asm mov EAX,_TimerStart /* pre-fetch cache line */ \
__asm push EDX \
__asm rdtsc \
__asm nop \
__asm mov _TimerStart,EAX \
__asm pop EDX \
__asm pop EAX \
__asm nop /* prevents AGI stall EAX/EDX/ESP */ \
__asm nop };
#define E __asm { \
__asm push EAX \
__asm push EDX \
__asm rdtsc \
__asm pushf \
__asm mov EDX,_TimerIndex \
__asm cmp EDX,_TimerCount /* stop counter after last index */ \
__asm mov _TimerEnd[EDX*4],EAX \
__asm mov EAX,_TimerStart \
__asm adc _TimerIndex,0 \
__asm mov _TimerBeg[EDX*4],EAX \
__asm popf \
__asm pop EDX \
__asm pop EAX };
static int _TimerBeg[_TimerCount + 1];
static int _TimerEnd[_TimerCount + 1];
static int _TimerIndex;
static int _TimerStart;
static struct _TimerAtExit {
~_TimerAtExit() {
char ac[1024];
char* pc = ac;
int iMin = 0x7FFFFFFF;
double rSum = 0;
double rUsed = 0;
double rTotal = 0;
for (int i = 0; i < _TimerCount; i++) {
int iClocks = ((_TimerEnd[i] - _TimerBeg[i]) & 0x7FFFFFFF) - 17;
if (iClocks < 0) {
break;
}
if (pc < ac + sizeof ac - 16) {
pc += wsprintf(pc, " %d", iClocks);
}
if (iClocks < iMin) {
iMin = iClocks;
}
rSum += iClocks;
if (i > 0) {
rUsed += iClocks;
rTotal += ((_TimerBeg[i] - _TimerBeg[i - 1]) & 0x7FFFFFFF) - 59;
}
}
if (i > 0) {
strcpy(pc, "\n");
OutputDebugString(ac);
if (i > 1) {
double rAvg = rSum / i;
double rDev = 0;
if (iMin != 0) {
rDev = (rAvg - iMin) * 100.0 / iMin;
}
double rPercent = rUsed * 100.0 / rTotal;
sprintf(ac, " %.6g clocks avg (%d clocks minimum + %.3g%%
deviation) consuming %.4g%% of CPU performance\n", rAvg, iMin, rDev,
rPercent); OutputDebugString(ac); }
}
}
Quote:
} _TimerAtExit;
int __cdecl main() {
for (int i = 0; i < 1000; i++) __asm {
B
////////////////////////////////////////////////////////////////////////
////////
nop
nop
////////////////////////////////////////////////////////////////////////
//////// E
}
return 0;
Quote:
}