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