Commit dd2a0be7 authored by Martin Marinov's avatar Martin Marinov

Moved and Rafactored StopWatch to Base/Utils. Added DEB_time_func and...

Moved and Rafactored StopWatch to Base/Utils. Added DEB_time_func and DEB_time_session macros in a new header Base/Debug/DebTime.hh

[git-p4: depot-paths = "//ReForm/ReForm/main/Base/": change = 11256]
parent b83eb676
set(my_headers
${CMAKE_CURRENT_SOURCE_DIR}/DebOut.hh
${CMAKE_CURRENT_SOURCE_DIR}/DebShow.hh
${CMAKE_CURRENT_SOURCE_DIR}/DebTime.hh
${CMAKE_CURRENT_SOURCE_DIR}/DebUtils.hh
PARENT_SCOPE
)
......
// (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.
#ifndef BASE_DEBTIME_HH_INCLUDED
#define BASE_DEBTIME_HH_INCLUDED
#include <Base/Utils/StopWatch.hh>
#include <Base/Debug/DebOut.hh>
namespace Base {
class StopWatchSession
{
public:
StopWatchSession(
ReForm::DebEnter& _deb,
const char* _sssn_name = nullptr,
const int _deb_lvl = 2)
: deb_(_deb), sssn_name_(_sssn_name), deb_lvl_(_deb_lvl)
{
sw_.start();
}
~StopWatchSession()
{
// TODO: implement "prettier" DEB out if seconds turn into minutes/hours/etc
if (!deb_.permission(deb_lvl_))
return;
deb_.stream() << sssn_name_ << " took " << sw_.stop()/1000.0 << " s.\n"
<< deb_.end();
}
private:
ReForm::DebEnter& deb_;
const char* sssn_name_;
const int deb_lvl_;
StopWatch sw_;
private:
// disable copy and assignment
StopWatchSession(const StopWatchSession&);
StopWatchSession& operator=(const StopWatchSession&);
};
} //namespace Base
#define DEB_time_session(SSSN, LL) \
Base::StopWatchSession __sw_sssn(deb, SSSN, LL);
#define DEB_time_session_def(SSSN) \
Base::StopWatchSession __sw_sssn(deb, SSSN, 2);
#define DEB_time_func(LL) DEB_enter_func \
Base::StopWatchSession __sw_func(deb, __FUNCTION__, LL);
#define DEB_time_func_def DEB_time_func(2)
#endif//BASE_DEBTIME_HH_INCLUDED
set(my_headers
${CMAKE_CURRENT_SOURCE_DIR}/OutcomeUtils.hh
${CMAKE_CURRENT_SOURCE_DIR}/StopWatch.hh
PARENT_SCOPE
)
set(my_sources
${CMAKE_CURRENT_SOURCE_DIR}/StopWatch.cc
PARENT_SCOPE
)
// (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"
#ifdef WIN32
// 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:
StopWatch()
{
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
//=============================================================================
//
// OpenFlipper
// Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
// www.openflipper.org
//
//-----------------------------------------------------------------------------
//
// License
//
// OpenFlipper is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenFlipper is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenFlipper. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
// $Revision: 3468 $
// $Author: moebius $
// $Date: 2008-10-17 14:58:52 +0200 (Fri, 17 Oct 2008) $
//
//=============================================================================
// (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.
//=============================================================================
//
// CLASS StopWatch
//
//=============================================================================
#ifndef BASE_STOPWATCH_HH_INCLUDED
#define BASE_STOPWATCH_HH_INCLUDED
namespace Base {
//== CLASS DEFINITION =========================================================
/** \class StopWatch StopWatch.hh
This class can be used for measuring time, providing optimal granularity
for the current platform.
**/
class StopWatch
{
public:
/// Constructor
StopWatch();
/// Destructor
~StopWatch();
/// Start time measurement
void start();
/// Restart, return time elapsed until now.
double restart();
/// Stop time measurement, return time.
double stop();
/// Get the total time in milli-seconds (watch has to be stopped).
double elapsed() const;
private:
class Impl;
Impl* impl_;
private:
// disable copy and assignment
StopWatch(const StopWatch&);
StopWatch& operator=(const StopWatch&);
};
//=============================================================================
} // namespace Base
//=============================================================================
#endif//BASE_STOPWATCH_HH_INCLUDED
//=============================================================================
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment