github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/webp/libwebp/examples/stopwatch.h (about)

     1  // Copyright 2011 Google Inc. All Rights Reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style license
     4  // that can be found in the COPYING file in the root of the source
     5  // tree. An additional intellectual property rights grant can be found
     6  // in the file PATENTS. All contributing project authors may
     7  // be found in the AUTHORS file in the root of the source tree.
     8  // -----------------------------------------------------------------------------
     9  //
    10  //  Helper functions to measure elapsed time.
    11  //
    12  // Author: Mikolaj Zalewski (mikolajz@google.com)
    13  
    14  #ifndef WEBP_EXAMPLES_STOPWATCH_H_
    15  #define WEBP_EXAMPLES_STOPWATCH_H_
    16  
    17  #if defined _WIN32 && !defined __GNUC__
    18  #include <windows.h>
    19  
    20  typedef LARGE_INTEGER Stopwatch;
    21  
    22  static WEBP_INLINE void StopwatchReset(Stopwatch* watch) {
    23    QueryPerformanceCounter(watch);
    24  }
    25  
    26  static WEBP_INLINE double StopwatchReadAndReset(Stopwatch* watch) {
    27    const LARGE_INTEGER old_value = *watch;
    28    LARGE_INTEGER freq;
    29    if (!QueryPerformanceCounter(watch))
    30      return 0.0;
    31    if (!QueryPerformanceFrequency(&freq))
    32      return 0.0;
    33    if (freq.QuadPart == 0)
    34      return 0.0;
    35    return (watch->QuadPart - old_value.QuadPart) / (double)freq.QuadPart;
    36  }
    37  
    38  
    39  #else    /* !_WIN32 */
    40  #include <sys/time.h>
    41  
    42  typedef struct timeval Stopwatch;
    43  
    44  static WEBP_INLINE void StopwatchReset(Stopwatch* watch) {
    45    gettimeofday(watch, NULL);
    46  }
    47  
    48  static WEBP_INLINE double StopwatchReadAndReset(Stopwatch* watch) {
    49    const struct timeval old_value = *watch;
    50    gettimeofday(watch, NULL);
    51    return watch->tv_sec - old_value.tv_sec +
    52        (watch->tv_usec - old_value.tv_usec) / 1000000.0;
    53  }
    54  
    55  #endif   /* _WIN32 */
    56  
    57  #endif  /* WEBP_EXAMPLES_STOPWATCH_H_ */