StopWatch.cc 2.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// (C) Copyright 2015 by Autodesk, Inc.
//
// The information contained herein is confidential, proprietary
// to Autodesk,  Inc.,  and considered a trade secret as defined
// in section 499C of the penal code of the State of California.
// Use of  this information  by  anyone  other  than  authorized
// employees of Autodesk, Inc.  is granted  only under a written
// non-disclosure agreement,  expressly  prescribing  the  scope
// and manner of such use.

#include <Base/Security/Mandatory.hh>
#include "StopWatch.hh"

14
#ifdef _MSC_VER
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

// Windows implementation
//////////////////////////////////////////////////////////////////////////

INSECURE_INCLUDE_SECTION_BEGIN
#include <windows.h>
INSECURE_INCLUDE_SECTION_END

namespace Base {

class StopWatch::Impl
{
public:
  Impl() 
  { 
    QueryPerformanceFrequency(&freq_); 
  }

  void start() 
  {
    QueryPerformanceCounter(&starttime_);
  }

  void stop()
  {
    QueryPerformanceCounter(&endtime_);
  }

  double elapsed() const 
  {
    return (double)(endtime_.QuadPart - starttime_.QuadPart) / 
      (double)freq_.QuadPart * 1000.0f;
  }

private:
  LARGE_INTEGER starttime_, endtime_;
  LARGE_INTEGER freq_;
};

} //namespace Base

#else 

// Linux implementation
//////////////////////////////////////////////////////////////////////////

#include <sys/time.h>

namespace Base {


class StopWatch::Impl
{
public:
  Impl()
  { 
    starttime_.tv_sec = starttime_.tv_usec = 0;
    endtime_.tv_sec   = endtime_.tv_usec = 0;
  }

  void start() 
  {
    starttime_ = current_time();
  }

  void stop() 
  {
    endtime_ = current_time();
  }

  /// Get the total time in milli-seconds (watch has to be stopped).
  double elapsed() const 
  {
    return ((endtime_.tv_sec  - starttime_.tv_sec )*1000.0 +
	    (endtime_.tv_usec - starttime_.tv_usec)*0.001);
  }

private:
  timeval starttime_, endtime_;

private:
  static timeval current_time()
  {
    struct timeval tv;
    gettimeofday(&tv, 0);
    return tv;
  }
};

} //namespace Base

#endif//

// StopWatch interface implementation
//////////////////////////////////////////////////////////////////////////

namespace Base {

StopWatch::StopWatch() 
  : impl_(new Impl) 
{}

StopWatch::~StopWatch() 
{ delete impl_; }

void StopWatch::start()
{
  impl_->start();
}

double StopWatch::restart()
{
  double t = stop(); 
  start();
  return t;
}

double StopWatch::stop()
{
  impl_->stop();
  return elapsed();
}

double StopWatch::elapsed() const
{
  return impl_->elapsed();
}

} // namespace Base