Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Win32 Performance Measurement Options


Listing Three: Extract from winstl_systemtime_counter.h


/* /////////////////////////////////////////////////////////////
 * ...
 *
 * Extract from winstl_systemtime_counter.h
 *
 * Copyright (C) 2002, Synesis Software Pty Ltd.
 * (Licensed under the Synesis Software Standard Source License:
 *  http://www.synesis.com.au/licenses/ssssl.html)
 *
 * ...
 * ////////////////////////////////////////////////////////// */

// Operations
inline void systemtime_counter::start()
{
    ::GetSystemTimeAsFileTime(reinterpret_cast<LPFILETIME>(&m_start));
}

inline void systemtime_counter::stop()
{
    ::GetSystemTimeAsFileTime(reinterpret_cast<LPFILETIME>(&m_end));
}

// Attributes
inline systemtime_counter::interval_type systemtime_counter::get_seconds() const
{
    return get_period_count() / interval_type(10000000);
}

inline systemtime_counter::interval_type systemtime_counter::get_milliseconds() const
{
    return get_period_count() / interval_type(10000);
}

inline systemtime_counter::interval_type systemtime_counter::get_microseconds() const
{
    return get_period_count() / interval_type(10);
}

Listing Four: Extract from winstl_highperformance_counter.h


/* /////////////////////////////////////////////////////////////
 * ...
 *
 * Extract from winstl_highperformance_counter.h
 *
 * Copyright (C) 2002, Synesis Software Pty Ltd.
 * (Licensed under the Synesis Software Standard Source License:
 *  http://www.synesis.com.au/licenses/ssssl.html)
 *
 * ...
 * ////////////////////////////////////////////////////////// */

inline /* static */ highperformance_counter::interval_type highperformance_counter::_query_frequency()
{
    interval_type   frequency;

    // If no high-performance counter is available ...
    if( !::QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*> (&frequency)) ||
        frequency == 0)
    {
        // ... then set the divisor to be the maximum value, guaranteeing that 
        // the timed periods will always evaluate to 0.
        frequency = stlsoft_ns_qual(limit_traits)<interval_type>::maximum();
    }

    return frequency;
}

inline /* static */ highperformance_counter::interval_type highperformance_counter::_frequency()
{
    static interval_type  s_frequency = _query_frequency();

    return s_frequency;
}

// Operations
inline void highperformance_counter::start()
{
    ::QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&m_start));
}

inline void highperformance_counter::stop()
{
    ::QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&m_end));
}

// Attributes
inline highperformance_counter::interval_type highperformance_counter::get_seconds() const
{
    return get_period_count() / _frequency();
}

inline highperformance_counter::interval_type highperformance_counter::get_milliseconds() const
{
    highperformance_counter::interval_type  result;
    highperformance_counter::interval_type  count   =   get_period_count();

    if(count < __STLSOFT_GEN_SINT64_SUFFIX(0x20C49BA5E353F7))
    {
        result = (count * interval_type(1000)) / _frequency();
    }
    else
    {
        result = (count / _frequency()) * interval_type(1000);
    }

    return result;
}

inline highperformance_counter::interval_type highperformance_counter::get_microseconds() const
{
    highperformance_counter::interval_type  result;
    highperformance_counter::interval_type  count   =   get_period_count();

    if(count < __STLSOFT_GEN_SINT64_SUFFIX(0x8637BD05AF6))
    {
        result = (count * interval_type(1000000)) / _frequency();
    }
    else
    {
        result = (count / _frequency()) * interval_type(1000000);
    }

    return result;
}


Listing Five: Extract from winstl_threadtimes_counter.h


/* /////////////////////////////////////////////////////////////
 * ...
 *
 * Extract from winstl_threadtimes_counter.h
 *
 * Copyright (C) 2002, Synesis Software Pty Ltd.
 * (Licensed under the Synesis Software Standard Source License:
 *  http://www.synesis.com.au/licenses/ssssl.html)
 *
 * ...
 * ////////////////////////////////////////////////////////// */

inline threadtimes_counter::threadtimes_counter()
    : m_thread(::GetCurrentThread())
{
}

// Operations
inline void threadtimes_counter::start()
{
    FILETIME    creationTime;
    FILETIME    exitTime;

    ::GetThreadTimes(   m_thread,
                        &creationTime,
                        &exitTime,
                        reinterpret_cast<LPFILETIME>(&m_kernelStart),
                        reinterpret_cast<LPFILETIME>(&m_userStart));
}

inline void threadtimes_counter::stop()
{
    FILETIME    creationTime;
    FILETIME    exitTime;

    ::GetThreadTimes(   m_thread,
                        &creationTime,
                        &exitTime,
                        reinterpret_cast<LPFILETIME>(&m_kernelEnd),
                        reinterpret_cast<LPFILETIME>(&m_userEnd));
}

// Attributes

// Kernel
inline threadtimes_counter::interval_type threadtimes_counter::get_kernel_period_count() const
{
    return static_cast<interval_type>(m_kernelEnd - m_kernelStart);
}

inline threadtimes_counter::interval_type threadtimes_counter::get_kernel_seconds() const
{
    return get_kernel_period_count() / interval_type(10000000);
}

inline threadtimes_counter::interval_type threadtimes_counter::get_kernel_milliseconds() const
{
    return get_kernel_period_count() / interval_type(10000);
}

inline threadtimes_counter::interval_type threadtimes_counter::get_kernel_microseconds() const
{
    return get_kernel_period_count() / interval_type(10);
}

// User
inline threadtimes_counter::interval_type threadtimes_counter::get_user_period_count() const
{
    return static_cast<interval_type>(m_userEnd - m_userStart);
}

inline threadtimes_counter::interval_type threadtimes_counter::get_user_seconds() const
{
    return get_user_period_count() / interval_type(10000000);
}

inline threadtimes_counter::interval_type threadtimes_counter::get_user_milliseconds() const
{
    return get_user_period_count() / interval_type(10000);
}

inline threadtimes_counter::interval_type threadtimes_counter::get_user_microseconds() const
{
    return get_user_period_count() / interval_type(10);
}

// Total
inline threadtimes_counter::interval_type threadtimes_counter::get_period_count() const
{
    return get_kernel_period_count() + get_user_period_count();
}

inline threadtimes_counter::interval_type threadtimes_counter::get_seconds() const
{
    return get_period_count() / interval_type(10000000);
}

inline threadtimes_counter::interval_type    threadtimes_counter::get_milliseconds() const
{
    return get_period_count() / interval_type(10000);
}

inline threadtimes_counter::interval_type    threadtimes_counter::get_microseconds() const
{
    return get_period_count() / interval_type(10);
}

Listing Six: Extract from winstl_processtimes_counter.h


/* /////////////////////////////////////////////////////////////
 * ...
 *
 * Extract from winstl_processtimes_counter.h
 *
 * Copyright (C) 2002, Synesis Software Pty Ltd.
 * (Licensed under the Synesis Software Standard Source License:
 *  http://www.synesis.com.au/licenses/ssssl.html)
 *
 * ...
 * ////////////////////////////////////////////////////////// */

inline /* static */ HANDLE processtimes_counter::_get_process_handle()
{
	static HANDLE	s_hProcess	=	::GetCurrentProcess();

	return s_hProcess;
}

// Operations
inline void processtimes_counter::start()
{
    FILETIME    creationTime;
    FILETIME    exitTime;

    ::GetProcessTimes(  _get_process_handle(),
                        &creationTime,
                        &exitTime,
                        reinterpret_cast<LPFILETIME>(&m_kernelStart),
                        reinterpret_cast<LPFILETIME>(&m_userStart));
}

inline void processtimes_counter::stop()
{
    FILETIME    creationTime;
    FILETIME    exitTime;

    ::GetProcessTimes(  _get_process_handle(),
                        &creationTime,
                        &exitTime,
                        reinterpret_cast<LPFILETIME>(&m_kernelEnd),
                        reinterpret_cast<LPFILETIME>(&m_userEnd));
}

The full implementations are provided in the archive and are available in their most up-to-date form online at http://winstl.org/.) They all have a similar form and semantics according to the following format:

  class xxx_counter
  {
  public:
    ...

    typedef ws_sint64_t epoch_type;
    typedef ws_sint64_t interval_type;

  // Operations
  public:
    void start();
    void stop();

  // Attributes
  public:
    interval_type get_period_count() const;
    interval_type get_seconds() const;
    interval_type get_milliseconds() const;
    interval_type get_microseconds() const;

    ...
};

By providing the same interface, they can easily be substituted (either by a single typedef change or as a result of preprocessor environment discrimination) to suit the needs of the program(mer).

The start() method causes the first timing instant to be recorded, and the stop() method causes the second timing instant to be recorded. start() and stop() can be called multiple times, allowing staged timings, although obviously you will get nonsense values from the period attributes if start() is called after calling stop(). (Indeed, this is the reason that the interval types are signed, so that such values are negative and can, therefore, be more easily spotted.) Each of the classes calculates the elapsed time from the difference between these two instant values.

The elapsed time for the measured period is provided by each class in units of seconds, milliseconds, and microseconds via the get_seconds(), get_milliseconds(), and get_microseconds() methods, respectively. The resolution of the return values from these methods depends on the underlying timing function; i.e., tick_counter's get_microseconds() will always return exactly 1000 times the value returned by get_milliseconds(), since GetTickCount()'s measurement resolution is (at best) 1 millisecond.

Each class also provides the get_period_ count() method, which returns the extent of the elapsed period — in timing function-specific increments — by calculating the difference between the start and stop instant values. This can be of use when doing relative performance measures, since this method generally has a lower performance cost than any of the elapsed time methods (because most of them have to perform additional multiplications/divisions in order to convert into time units).

The methods of all the classes are implemented inline for maximum efficiency. (Examination of the generated object code has shown that the inlining is carried out, and there is no significant additional overhead when using the class implementations over the Win32 functions directly.) Furthermore, having all the methods as inline simplifies use of the library since there are no implementation files to compile and link. Where pertinent, late-evaluation (also known as lazy-evaluation) techniques and static members are used so that the costs of calls (such as to GetCurrentProcess()) are only incurred once, and only when their information is actually needed.

tick_counter and multimedia_counter — tick_ counter and multimedia_counter record the 32-bit unsigned values returned by GetTickCount() and timeGetTime(), respectively, in the start() and stop() methods into their m_start and m_end members. get_milliseconds() simply returns get_period_count(), get_microseconds() returns get_period_count() multiplied by 1000, and get_seconds() returns get_period_count() divided by 1000.

systemtime_counter — systemtime_counter records the FILETIME value obtained from GetSystemTimeAsFileTime() in its start() and stop() methods, converting to ws_sint64_t (see the section "Win32 64-Bit Integers"). get_period_count() returns a value in 100ns increments, so get_seconds(), get_milliseconds(), and get_microseconds() are implemented to return this value divided by 10,000,000, 10000, and 10, respectively. GetSystemTimeAsFileTime() is preferred over GetSystemTime() (since it exists on all platforms save CE), is far more efficient on NT, and affords a simple and cleaner implementation of the class.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.