Monitoring Within an Application

Now that you know what is involved in writing a performance monitor, you may want to measure your own application to get some understanding of how it is operating. You can display the data when your application quits.

One thing you can do is simply run the ntimer command included on the floppy disk provided with this book. Typing the following will start the application you want to time, and then when the application ends, it prints basic execution statistics of elapsed, user, and kernel time in the application process.

ntimer application

For looking inside your application, the high resolution performance timer on the system is read using the QueryPerformanceCounter API call. This returns a 64-bit counter that contains the current count. The resolution of this counter varies from one system to the next. You can use the QueryPerformanceFrequency call to get this frequency on the system you are running on. Peruse the API documentation for the details.

A couple of other calls may also be handy for you. These are GetProcessTimes and GetThreadTimes. These return the same information printed by the ntimer utility. Realize that these timers are all subject to the granularity of the system clock. Even though the values returned are in 100-nanosecond units, the system clocks on the 486 and MIPS tick at 10-millisecond intervals, and on the MIPS at 10-millisecond intervals. So calling these more frequently within a program will not yield accurate results. Beware misleading advertising!