1 // Copyright 2015 Google Inc. All rights reserved
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // +build ignore
16 
17 #include "timeutil.h"
18 
19 #include <sys/time.h>
20 #include <time.h>
21 
22 #include "log.h"
23 
GetTime()24 double GetTime() {
25 #if defined(__linux__)
26   struct timespec ts;
27   clock_gettime(CLOCK_REALTIME, &ts);
28   return ts.tv_sec + ts.tv_nsec * 0.001 * 0.001 * 0.001;
29 #else
30   struct timeval tv;
31   if (gettimeofday(&tv, NULL) < 0)
32     PERROR("gettimeofday");
33   return tv.tv_sec + tv.tv_usec * 0.001 * 0.001;
34 #endif
35 }
36 
ScopedTimeReporter(const char * name)37 ScopedTimeReporter::ScopedTimeReporter(const char* name)
38     : name_(name), start_(GetTime()) {}
39 
~ScopedTimeReporter()40 ScopedTimeReporter::~ScopedTimeReporter() {
41   double elapsed = GetTime() - start_;
42   LOG_STAT("%s: %f", name_, elapsed);
43 }
44