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