From 176b1073c6f3d61cc14a8cac83a52ebf02cf571c Mon Sep 17 00:00:00 2001 From: a7458969 <290198252@qq.com> Date: Fri, 22 May 2020 11:17:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=B8=AA=E9=AB=98?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E5=AE=9A=E6=97=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- general/CMakeLists.txt | 2 +- general/src/function/Timer.cpp | 132 +++++++++++++++++++++++++++++++++ general/src/function/Timer.h | 56 ++++++++++++++ 3 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 general/src/function/Timer.cpp create mode 100644 general/src/function/Timer.h diff --git a/general/CMakeLists.txt b/general/CMakeLists.txt index 2cbcde7..0011556 100644 --- a/general/CMakeLists.txt +++ b/general/CMakeLists.txt @@ -35,7 +35,7 @@ aux_source_directory(src/pattern PaternSrc) add_library(General OBJECT ${DIRSRCS} ${PaternSrc} src/pattern/signleton.hpp src/pattern/Observer.hpp src/pattern/stratergy.hpp "src/pattern/adapter.hpp" src/encrypt/base64.cpp src/encrypt/base64.h src/encrypt/aes.cpp src/encrypt/aes.h src/encrypt/rsa.cpp - src/math/BigInt.hpp src/net/TcpClient.cpp src/net/TcpClient.h src/net/PackageReceiver.cpp src/net/PackageReceiver.h src/function/btree.cpp src/function/btree.h src/algorithm/sorter.hpp) + src/math/BigInt.hpp src/net/TcpClient.cpp src/net/TcpClient.h src/net/PackageReceiver.cpp src/net/PackageReceiver.h src/function/Timer.cpp src/function/btree.cpp src/function/btree.h src/algorithm/sorter.hpp) set(COPYITEM inc) file(GLOB INCLUDES ${PROJECT_SOURCE_DIR}/inc/*) diff --git a/general/src/function/Timer.cpp b/general/src/function/Timer.cpp new file mode 100644 index 0000000..84bd80c --- /dev/null +++ b/general/src/function/Timer.cpp @@ -0,0 +1,132 @@ +////////////////////////////////////////////////////////////////////////////// +// Timer.cpp +// ========= +// High Resolution Timer. +// This timer is able to measure the elapsed time with 1 micro-second accuracy +// in both Windows, Linux and Unix system +// +// AUTHOR: Song Ho Ahn (song.ahn@gmail.com) +// CREATED: 2003-01-13 +// UPDATED: 2017-03-30 +// +// Copyright (c) 2003 Song Ho Ahn +////////////////////////////////////////////////////////////////////////////// + +#include "Timer.h" +#include + +/////////////////////////////////////////////////////////////////////////////// +// constructor +/////////////////////////////////////////////////////////////////////////////// +Timer::Timer() +{ +#if defined(WIN32) || defined(_WIN32) + QueryPerformanceFrequency(&frequency); + startCount.QuadPart = 0; + endCount.QuadPart = 0; +#else + startCount.tv_sec = startCount.tv_usec = 0; + endCount.tv_sec = endCount.tv_usec = 0; +#endif + + stopped = 0; + startTimeInMicroSec = 0; + endTimeInMicroSec = 0; +} + + + +/////////////////////////////////////////////////////////////////////////////// +// distructor +/////////////////////////////////////////////////////////////////////////////// +Timer::~Timer() +{ +} + + + +/////////////////////////////////////////////////////////////////////////////// +// start timer. +// startCount will be set at this point. +/////////////////////////////////////////////////////////////////////////////// +void Timer::start() +{ + stopped = 0; // reset stop flag +#if defined(WIN32) || defined(_WIN32) + QueryPerformanceCounter(&startCount); +#else + gettimeofday(&startCount, NULL); +#endif +} + + + +/////////////////////////////////////////////////////////////////////////////// +// stop the timer. +// endCount will be set at this point. +/////////////////////////////////////////////////////////////////////////////// +void Timer::stop() +{ + stopped = 1; // set timer stopped flag + +#if defined(WIN32) || defined(_WIN32) + QueryPerformanceCounter(&endCount); +#else + gettimeofday(&endCount, NULL); +#endif +} + + + +/////////////////////////////////////////////////////////////////////////////// +// compute elapsed time in micro-second resolution. +// other getElapsedTime will call this first, then convert to correspond resolution. +/////////////////////////////////////////////////////////////////////////////// +double Timer::getElapsedTimeInMicroSec() +{ +#if defined(WIN32) || defined(_WIN32) + if(!stopped) + QueryPerformanceCounter(&endCount); + + startTimeInMicroSec = startCount.QuadPart * (1000000.0 / frequency.QuadPart); + endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart); +#else + if(!stopped) + gettimeofday(&endCount, NULL); + + startTimeInMicroSec = (startCount.tv_sec * 1000000.0) + startCount.tv_usec; + endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec; +#endif + + return endTimeInMicroSec - startTimeInMicroSec; +} + + + +/////////////////////////////////////////////////////////////////////////////// +// divide elapsedTimeInMicroSec by 1000 +/////////////////////////////////////////////////////////////////////////////// +double Timer::getElapsedTimeInMilliSec() +{ + return this->getElapsedTimeInMicroSec() * 0.001; +} + + + +/////////////////////////////////////////////////////////////////////////////// +// divide elapsedTimeInMicroSec by 1000000 +/////////////////////////////////////////////////////////////////////////////// +double Timer::getElapsedTimeInSec() +{ + return this->getElapsedTimeInMicroSec() * 0.000001; +} + + + +/////////////////////////////////////////////////////////////////////////////// +// same as getElapsedTimeInSec() +/////////////////////////////////////////////////////////////////////////////// +double Timer::getElapsedTime() +{ + return this->getElapsedTimeInSec(); +} diff --git a/general/src/function/Timer.h b/general/src/function/Timer.h new file mode 100644 index 0000000..d24ab52 --- /dev/null +++ b/general/src/function/Timer.h @@ -0,0 +1,56 @@ +////////////////////////////////////////////////////////////////////////////// +// Timer.h +// ======= +// High Resolution Timer. +// This timer is able to measure the elapsed time with 1 micro-second accuracy +// in both Windows, Linux and Unix system +// +// AUTHOR: Song Ho Ahn (song.ahn@gmail.com) +// CREATED: 2003-01-13 +// UPDATED: 2017-03-30 +// +// Copyright (c) 2003 Song Ho Ahn +////////////////////////////////////////////////////////////////////////////// + +#ifndef TIMER_H_DEF +#define TIMER_H_DEF + +#if defined(WIN32) || defined(_WIN32) // Windows system specific +#include +#else // Unix based system specific +#include +#endif + + +class Timer +{ +public: + Timer(); // default constructor + ~Timer(); // default destructor + + void start(); // start timer + void stop(); // stop the timer + double getElapsedTime(); // get elapsed time in second + double getElapsedTimeInSec(); // get elapsed time in second (same as getElapsedTime) + double getElapsedTimeInMilliSec(); // get elapsed time in milli-second + double getElapsedTimeInMicroSec(); // get elapsed time in micro-second + + +protected: + + +private: + double startTimeInMicroSec; // starting time in micro-second + double endTimeInMicroSec; // ending time in micro-second + int stopped; // stop flag +#if defined(WIN32) || defined(_WIN32) + LARGE_INTEGER frequency; // ticks per second + LARGE_INTEGER startCount; // + LARGE_INTEGER endCount; // +#else + timeval startCount; // + timeval endCount; // +#endif +}; + +#endif // TIMER_H_DEF