blob: 0b43bf4ca128354f72d52872b638cb6df7b90b69 [file] [log] [blame]
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +09001// 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
Shinichiro Hamajicbd34cd2015-07-05 02:53:04 +090017#include "timeutil.h"
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +090018
Shinichiro Hamaji55f9d6d2016-01-14 08:19:27 +090019#include <sys/time.h>
Dan Willemsen19edaf12016-01-06 17:43:58 -080020#include <time.h>
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +090021
22#include "log.h"
23
24double GetTime() {
Shinichiro Hamajia69cc2c2015-07-31 15:21:08 +090025#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
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +090030 struct timeval tv;
31 if (gettimeofday(&tv, NULL) < 0)
32 PERROR("gettimeofday");
33 return tv.tv_sec + tv.tv_usec * 0.001 * 0.001;
Shinichiro Hamajia69cc2c2015-07-31 15:21:08 +090034#endif
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +090035}
36
37ScopedTimeReporter::ScopedTimeReporter(const char* name)
Dan Willemsen3ce083f2017-10-11 22:17:48 -070038 : name_(name), start_(GetTime()) {}
Shinichiro Hamaji54e52dd2015-06-27 17:14:06 +090039
40ScopedTimeReporter::~ScopedTimeReporter() {
41 double elapsed = GetTime() - start_;
42 LOG_STAT("%s: %f", name_, elapsed);
43}