RDTSC on Pentium and Pentium Pro for Timing Accuracy 
Author Message
 RDTSC on Pentium and Pentium Pro for Timing Accuracy

Hello everyone,

I'm using the RDTSC instruction on Pentium and Pentium Pro
microprocessors, which returns a 64-bit cycle counter. To time a
portion of code, I do something like...

DWORDLONG GetTimeStamp(VOID) {
  __asm {
    RDTSC     ; Visual C++ inline assembly
  }

Quote:
}

...
DWORDLONG start = GetTimeStamp();
// do some code
DWORDLONG end = GetTimeStamp();
...

Then I just do (end - start) / (cycles per second of processor) to
determine how many seconds have elapsed, for example. My main purpose
of using this is to determine how many microseconds or nanoseconds
that have elapsed after executing some code.

To determine the number of cycles per second of the processor, I do...

DWORDLONG start = GetTimeStamp();
Sleep(1000); // Win32 API to sleep one second
DWORDLONG end = GetTimeStamp();

Is this logic correct? My goal is to determine how many microseconds
or nanoseconds have elapsed for a portion of code.

Thanks a lot,

Spencer Low
--

LowTek Creations < http://www.*-*-*.com/ ;



Thu, 24 Jun 1999 03:00:00 GMT  
 RDTSC on Pentium and Pentium Pro for Timing Accuracy

...

Quote:
> To determine the number of cycles per second of the processor, I do...

> DWORDLONG start = GetTimeStamp();
> Sleep(1000); // Win32 API to sleep one second
> DWORDLONG end = GetTimeStamp();

> Is this logic correct? My goal is to determine how many microseconds
> or nanoseconds have elapsed for a portion of code.

I would modify the code slightly, by pairing the GetTimeStamp calls with
calls to the OS asking for the highest-precision real-time timestamp
available, i.e. something like:

  clocks = GetTimeStamp();
  time = GetCurrentTime();  /* double prec seconds */
  Sleep(1000); /* This will only be approximate */
  clocks = GetTimeStamp() - clocks;
  time = GetCurrentTime() - time;

  clocksPrSecond = (double) clocks / time;

I don't know the name of the best API to use to implement
'GetCurrentTime', but you should be able to find several, either in the
OS kernal or in the MultiMedia extensions.

Terje

--

Using self-discipline, see http://www.eiffel.com/discipline
"almost all programming can be viewed as an exercise in caching"



Thu, 24 Jun 1999 03:00:00 GMT  
 RDTSC on Pentium and Pentium Pro for Timing Accuracy

Quote:


> ...
> > To determine the number of cycles per second of the processor, I do...

> > DWORDLONG start = GetTimeStamp();
> > Sleep(1000); // Win32 API to sleep one second
> > DWORDLONG end = GetTimeStamp();

> > Is this logic correct? My goal is to determine how many microseconds
> > or nanoseconds have elapsed for a portion of code.

> I would modify the code slightly, by pairing the GetTimeStamp calls with
> calls to the OS asking for the highest-precision real-time timestamp
> available, i.e. something like:

>   clocks = GetTimeStamp();
>   time = GetCurrentTime();  /* double prec seconds */
>   Sleep(1000); /* This will only be approximate */
>   clocks = GetTimeStamp() - clocks;
>   time = GetCurrentTime() - time;

>   clocksPrSecond = (double) clocks / time;

> I don't know the name of the best API to use to implement
> 'GetCurrentTime', but you should be able to find several, either in the
> OS kernal or in the MultiMedia extensions.

Thanks for the reply. The best I can currently find in the Win32 API
is GetTickCount() (accurate to milliseconds), since
QueryPerformanceCounter() doesn't seem to be implemented on my
systems.

Thanks again,

Spencer
--

LowTek Creations <http://www.lowtek.com/>



Fri, 25 Jun 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Pentium/Pentium Pro simulator

2. Pentium/Pentium Pro simulator

3. Pentium Pro & Pentium II instruction decomposition (uops)

4. Optimized Pentium code on a Pentium Pro

5. Pentium Pro not faster than Pentium?

6. Speed on Pentium Pro vs Pentium?

7. Pentium II vs. Pentium Pro

8. Pentium classic, Pentium MMX, Pentium Pro, ... (asm) INSTRUCTION SET wanted

9. Pentium classic, Pentium MMX, Pentium Pro, ... (asm) INSTRUCTION SET wanted

10. Pentium FPU accuracy

11. RDTSC hangs my Pentium???

12. RDTSC on Mobile Pentium III: No longer accurate

 

 
Powered by phpBB® Forum Software