00001 // @file timer.h 00002 00003 /* Copyright 2010 10gen Inc. 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #pragma once 00019 00020 #include "time_support.h" 00021 00022 namespace mongo { 00023 00027 class Timer { 00028 public: 00029 Timer() { 00030 reset(); 00031 } 00032 00033 Timer( unsigned long long start ) { 00034 old = start; 00035 } 00036 00037 int seconds() const { 00038 return (int)(micros() / 1000000); 00039 } 00040 00041 int millis() const { 00042 return (long)(micros() / 1000); 00043 } 00044 00045 unsigned long long micros() const { 00046 unsigned long long n = curTimeMicros64(); 00047 return n - old; 00048 } 00049 00050 unsigned long long micros(unsigned long long & n) const { // returns cur time in addition to timer result 00051 n = curTimeMicros64(); 00052 return n - old; 00053 } 00054 00055 unsigned long long startTime() { 00056 return old; 00057 } 00058 00059 void reset() { 00060 old = curTimeMicros64(); 00061 } 00062 00063 private: 00064 unsigned long long old; 00065 }; 00066 00067 } // namespace mongo