Cycle-exact Pentium timing routine for MSVC 4.2 
Author Message
 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:
}



Sat, 15 May 1999 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. MSVC 4.2 re-arranges inline asm

2. FPS 4.0 + MSVC 4.2 DLL problem

3. HELP! GraphWidget/Win95/MSVC V4/Tk 4.2

4. RDTSC on Pentium and Pentium Pro for Timing Accuracy

5. exact solution for scattering from a sphere routine

6. MSVC CHOKES on this ASM putpixel routine!

7. Counting Clock Cycles on Pentium Under Linux

8. MSVC CHOKES on this ASM putpixel routine!

9. Pentium MMX Cycle counts

10. Number of clock cycles for instructions on pentium

11. Pentium Cycle counts: where?

12. Monitoring with exact timing (Comparison Verilog/VHDL)

 

 
Powered by phpBB® Forum Software