Shinichiro Hamaji | 54e52dd | 2015-06-27 17:14:06 +0900 | [diff] [blame] | 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 | |
Shinichiro Hamaji | cbd34cd | 2015-07-05 02:53:04 +0900 | [diff] [blame] | 17 | #include "timeutil.h" |
Shinichiro Hamaji | 54e52dd | 2015-06-27 17:14:06 +0900 | [diff] [blame] | 18 | |
Shinichiro Hamaji | 55f9d6d | 2016-01-14 08:19:27 +0900 | [diff] [blame] | 19 | #include <sys/time.h> |
Dan Willemsen | 19edaf1 | 2016-01-06 17:43:58 -0800 | [diff] [blame] | 20 | #include <time.h> |
Shinichiro Hamaji | 54e52dd | 2015-06-27 17:14:06 +0900 | [diff] [blame] | 21 | |
| 22 | #include "log.h" |
| 23 | |
| 24 | double GetTime() { |
Shinichiro Hamaji | a69cc2c | 2015-07-31 15:21:08 +0900 | [diff] [blame] | 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 |
Shinichiro Hamaji | 54e52dd | 2015-06-27 17:14:06 +0900 | [diff] [blame] | 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; |
Shinichiro Hamaji | a69cc2c | 2015-07-31 15:21:08 +0900 | [diff] [blame] | 34 | #endif |
Shinichiro Hamaji | 54e52dd | 2015-06-27 17:14:06 +0900 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | ScopedTimeReporter::ScopedTimeReporter(const char* name) |
| 38 | : name_(name), start_(GetTime()) { |
| 39 | } |
| 40 | |
| 41 | ScopedTimeReporter::~ScopedTimeReporter() { |
| 42 | double elapsed = GetTime() - start_; |
| 43 | LOG_STAT("%s: %f", name_, elapsed); |
| 44 | } |