2012-06-11 02:59:04 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Copyright 2013 Pixar
|
2012-06-11 02:59:04 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
|
|
|
// with the following modification; you may not use this file except in
|
|
|
|
// compliance with the Apache License and the following modification to it:
|
|
|
|
// Section 6. Trademarks. is deleted and replaced with:
|
2012-06-11 02:59:04 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// 6. Trademarks. This License does not grant permission to use the trade
|
|
|
|
// names, trademarks, service marks, or product names of the Licensor
|
|
|
|
// and its affiliates, except as required to comply with Section 4(c) of
|
|
|
|
// the License and to reproduce the content of the NOTICE file.
|
2012-06-11 02:59:04 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// You may obtain a copy of the Apache License at
|
2012-06-11 02:59:04 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2013-07-18 21:19:50 +00:00
|
|
|
//
|
2013-09-26 19:04:57 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the Apache License with the above modification is
|
|
|
|
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
// KIND, either express or implied. See the Apache License for the specific
|
|
|
|
// language governing permissions and limitations under the Apache License.
|
2012-06-11 02:59:04 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef STOPWATCH_H
|
|
|
|
#define STOPWATCH_H
|
|
|
|
|
2016-02-19 09:02:07 +00:00
|
|
|
#if (_WIN32 || _WIN64)
|
2015-09-30 18:20:03 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2012-06-11 02:59:04 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class Stopwatch {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
#ifndef _WINDOWS
|
2014-09-05 22:07:46 +00:00
|
|
|
Stopwatch() : _totalElapsed(0) { }
|
|
|
|
|
2012-08-04 02:51:27 +00:00
|
|
|
void Start() {
|
2012-06-11 02:59:04 +00:00
|
|
|
struct timeval l_rtime;
|
|
|
|
gettimeofday(&l_rtime,0);
|
2012-08-04 02:51:27 +00:00
|
|
|
_elapsed = l_rtime.tv_sec + l_rtime.tv_usec/1000000.0;
|
2012-06-11 02:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Stop() {
|
|
|
|
struct timeval l_rtime;
|
|
|
|
|
|
|
|
gettimeofday(&l_rtime,0);
|
2012-08-04 02:51:27 +00:00
|
|
|
_elapsed = (l_rtime.tv_sec + l_rtime.tv_usec/1000000.0) - _elapsed;
|
2012-06-11 02:59:04 +00:00
|
|
|
_totalElapsed += _elapsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
double GetElapsed() const {
|
|
|
|
return _elapsed;
|
|
|
|
}
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-06-11 02:59:04 +00:00
|
|
|
double GetTotalElapsed() const {
|
|
|
|
return _totalElapsed;
|
2012-08-04 02:51:27 +00:00
|
|
|
}
|
2012-06-11 02:59:04 +00:00
|
|
|
#else
|
2014-09-05 22:07:46 +00:00
|
|
|
Stopwatch() : _totalElapsed(0) {
|
2012-06-15 18:41:16 +00:00
|
|
|
QueryPerformanceFrequency(&_frequency);
|
|
|
|
}
|
2012-08-04 02:51:27 +00:00
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
2012-06-15 18:41:16 +00:00
|
|
|
QueryPerformanceCounter(&_time);
|
2012-06-11 02:59:04 +00:00
|
|
|
}
|
|
|
|
|
2012-08-04 02:51:27 +00:00
|
|
|
void Stop()
|
|
|
|
{
|
2012-06-15 18:41:16 +00:00
|
|
|
LARGE_INTEGER currentTime;
|
|
|
|
QueryPerformanceCounter(¤tTime);
|
|
|
|
_elapsed = currentTime.QuadPart - _time.QuadPart;
|
|
|
|
_totalElapsed+=_elapsed;
|
2012-06-11 02:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double GetElapsed() const {
|
2012-06-15 18:41:16 +00:00
|
|
|
return (double) _elapsed / _frequency.QuadPart;
|
2012-06-11 02:59:04 +00:00
|
|
|
}
|
2012-08-04 02:51:27 +00:00
|
|
|
|
2012-06-11 02:59:04 +00:00
|
|
|
double GetTotalElapsed() const {
|
2012-06-15 18:41:16 +00:00
|
|
|
return (double) _totalElapsed / _frequency.QuadPart;
|
2012-06-11 02:59:04 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
#ifndef _WINDOWS
|
|
|
|
double _elapsed;
|
|
|
|
double _totalElapsed;
|
|
|
|
#else
|
2012-06-19 19:24:14 +00:00
|
|
|
LARGE_INTEGER _time;
|
2012-06-15 18:41:16 +00:00
|
|
|
LARGE_INTEGER _frequency;
|
2012-06-11 02:59:04 +00:00
|
|
|
__int64 _elapsed;
|
|
|
|
__int64 _totalElapsed;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* STOPWATCH_H */
|
|
|
|
|