| March 1, 2009 11:00 PM PST | |
Include code in an application to determine the clock speed of a mobile processor. This technique is increasingly valuable to independent software vendors as they customize their code for Intel® mobile processors.
Use the QTimeClass. The explanation that follows presents a fairly accurate way to measure CPU frequency by using the RDTSC (time-stamp counter) and computing the number of clock ticks per microsecond. The QTime Class is a C++ class that has three external methods:
void GetCPUTicsPerUSec(__int64 *pTickPerUSec); |
The GetTicksPerUSec method is the key to the QTimeClass. The first thing this method does is to read the time-stamp counter and to set the reading-process priority to realtime and the thread priority to time critical. The next step in calculating the number of ticks per microsecond is to read the timestamp counter as fast as possible, making sure that you have a difference in the start and stop times:
for (;;) |
Next, use the system function call timeGetTime, which has an incremental value of one millisecond. Wait for 100 milliseconds before setting the thread priority back to normal:
for (;;) |
At that point, you can take the difference between the first reading and the second reading and divide it by the elapsed time calculated from the timeGetTime system call, after converting to units of microseconds:
m_CPUTicksPerMicroSecond = (diff) / (elapsedTime * 1000); |
This technique yields a good estimate of the number of ticks per microsecond. You can save this value to calculate elapsed time in the GetTime method of this class.
Writing Code to Reveal the Performance Details of Mobile Processors
For more complete information about compiler optimizations, see our Optimization Notice.

