2015-07-11 18:56:02 +08:00
|
|
|
/*
|
2022-07-19 19:40:43 +08:00
|
|
|
Copyright (c) 2008-2022 Jan W. Krieger (<jan@jkrieger.de>) (DKFZ) & IWR, University of Heidelberg
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
last modification: $LastChangedDate: 2015-06-10 19:19:10 +0200 (Mi, 10 Jun 2015) $ (revision $Rev: 3976 $)
|
|
|
|
|
|
|
|
This software is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License (LGPL) as published by
|
2019-02-08 00:24:46 +08:00
|
|
|
the Free Software Foundation, either version 2.1 of the License, or
|
2015-07-11 18:56:02 +08:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 (LGPL) for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License (LGPL)
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
2019-11-24 19:21:06 +08:00
|
|
|
#include <chrono>
|
2015-07-11 18:56:02 +08:00
|
|
|
|
2019-06-22 20:21:32 +08:00
|
|
|
#include "jkqtcommon_imexport.h"
|
2019-06-20 21:18:58 +08:00
|
|
|
|
2015-07-11 18:56:02 +08:00
|
|
|
#ifndef __WINDOWS__
|
|
|
|
# if defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)
|
|
|
|
# define __WINDOWS__
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __LINUX__
|
|
|
|
# if defined(linux)
|
|
|
|
# define __LINUX__
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__WINDOWS__)
|
|
|
|
#include<windows.h>
|
|
|
|
#elif defined(__LINUX__)
|
|
|
|
#include <sys/time.h>
|
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef JKQTPHIGHRESTIMER_H
|
|
|
|
#define JKQTPHIGHRESTIMER_H
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief This class implements a high resolution timer capable of measuring time intervals with a resolution
|
|
|
|
* of some microseconds
|
2019-01-13 01:53:16 +08:00
|
|
|
* \ingroup jkqtptools_debugging
|
2015-07-11 18:56:02 +08:00
|
|
|
*
|
|
|
|
* \attention Note that this is a MS Windows specific implementation od a high-resolution timer using some of
|
|
|
|
* windows' API methods (namely \c QueryPerformanceCounter() and \c QueryPerformanceFrequency() ). So if you want
|
|
|
|
* to use this class on non-win32 systems you will have to find a way to implement it for your system!!!
|
|
|
|
*
|
|
|
|
* \attention Also note that a standard windows system is NOT a real time OS. So do not expect to get a high accuracy
|
|
|
|
* when timing operations using this timer. It gives you an accurate time stamp, but it can not guarantee when
|
|
|
|
* the code is executed!
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* The timer works very simple:
|
|
|
|
* - you can start the timer with start() which means you set a time=0
|
2019-01-26 20:00:40 +08:00
|
|
|
* - then you can query the time difference to the last call of start() by using getTime().
|
2015-07-11 18:56:02 +08:00
|
|
|
* .
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* \par win32 implementation issues:
|
|
|
|
* To implement this timer on windows systems I use two API calls from the windows kernel. They are:
|
|
|
|
* - <a href="http://msdn2.microsoft.com/en-us/library/ms644904.aspx">QueryPerformanceCounter()</a>
|
|
|
|
* - <a href="http://msdn2.microsoft.com/en-us/library/ms644905.aspx">QueryPerformanceFrequency()</a>
|
|
|
|
* .
|
|
|
|
* \n
|
|
|
|
* The first one is used to read times: In the start() method we simply save the current counter value to a variable.
|
2019-01-26 20:00:40 +08:00
|
|
|
* In getTime() we can then again use QueryPerformanceCounter() to get the current counter value and then calculate
|
2015-07-11 18:56:02 +08:00
|
|
|
* the difference between these two. Using QueryPerformanceFrequency() we can calculate the time difference in usecs
|
|
|
|
* from the counter value difference using:
|
|
|
|
* \f[ \Delta t=\frac{N_{\mbox{now}}-N_{\mbox{start}}}{\mbox{\texttt{QueryPerformanceFrequency()}}}\cdot 10^{6} \f]
|
|
|
|
*
|
|
|
|
*/
|
2019-06-22 20:21:32 +08:00
|
|
|
class JKQTCOMMON_LIB_EXPORT JKQTPHighResTimer {
|
2015-07-11 18:56:02 +08:00
|
|
|
protected:
|
2020-08-26 18:58:23 +08:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
/** \brief internal: time stamp of the last call of start() */
|
|
|
|
LARGE_INTEGER last;
|
|
|
|
#else
|
|
|
|
std::chrono::system_clock::time_point last;
|
|
|
|
#endif
|
|
|
|
/** \brief internal: timer frequency */
|
|
|
|
double freq;
|
2019-02-08 00:24:46 +08:00
|
|
|
public:
|
|
|
|
/** \brief class constructor. */
|
2015-07-11 18:56:02 +08:00
|
|
|
JKQTPHighResTimer();
|
2019-02-08 00:24:46 +08:00
|
|
|
/** \brief class destructor */
|
2015-07-11 18:56:02 +08:00
|
|
|
~JKQTPHighResTimer();
|
2019-02-08 00:24:46 +08:00
|
|
|
/** \brief start the timer */
|
|
|
|
void start();
|
|
|
|
/** \brief get the time since the last call of start() in microseconds */
|
|
|
|
double getTime();
|
2015-07-11 18:56:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // JKQTPHIGHRESTIMER_H
|