stopwatch.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #ifndef WEBP_EXAMPLES_STOPWATCH_H_
  14. #define WEBP_EXAMPLES_STOPWATCH_H_
  15. #include "webp/types.h"
  16. #if defined _WIN32 && !defined __GNUC__
  17. #include <windows.h>
  18. typedef LARGE_INTEGER Stopwatch;
  19. static WEBP_INLINE void StopwatchReset(Stopwatch* watch) {
  20. QueryPerformanceCounter(watch);
  21. }
  22. static WEBP_INLINE double StopwatchReadAndReset(Stopwatch* watch) {
  23. const LARGE_INTEGER old_value = *watch;
  24. LARGE_INTEGER freq;
  25. if (!QueryPerformanceCounter(watch))
  26. return 0.0;
  27. if (!QueryPerformanceFrequency(&freq))
  28. return 0.0;
  29. if (freq.QuadPart == 0)
  30. return 0.0;
  31. return (watch->QuadPart - old_value.QuadPart) / (double)freq.QuadPart;
  32. }
  33. #else /* !_WIN32 */
  34. #include <string.h> // memcpy
  35. #include <sys/time.h>
  36. typedef struct timeval Stopwatch;
  37. static WEBP_INLINE void StopwatchReset(Stopwatch* watch) {
  38. gettimeofday(watch, NULL);
  39. }
  40. static WEBP_INLINE double StopwatchReadAndReset(Stopwatch* watch) {
  41. struct timeval old_value;
  42. double delta_sec, delta_usec;
  43. memcpy(&old_value, watch, sizeof(old_value));
  44. gettimeofday(watch, NULL);
  45. delta_sec = (double)watch->tv_sec - old_value.tv_sec;
  46. delta_usec = (double)watch->tv_usec - old_value.tv_usec;
  47. return delta_sec + delta_usec / 1000000.0;
  48. }
  49. #endif /* _WIN32 */
  50. #endif // WEBP_EXAMPLES_STOPWATCH_H_