Universal Time test functions under Windows and Linux platforms

Source: Internet
Author: User

Time.cpp

////////////////////////////////////////////////////////////////////////////////Timer.cpp//=========//High Resolution Timer.//This timer was able to measure the elapsed time with 1 micro-second accuracy//In both Windows, Linux and Unix system////Author:song Ho Ahn ([email protected])//Created:2003-01-13//Updated:2006-01-13////Copyright (c) 2003 Song Ho Ahn//////////////////////////////////////////////////////////////////////////////#include"Timer.h"#include <stdlib.h>/////////////////////////////////////////////////////////////////////////////////Constructor///////////////////////////////////////////////////////////////////////////////Timer::timer () {#ifdef WIN32 queryperformancefrequency (&frequency); Startcount.quadpart =0; Endcount.quadpart =0;#elseStartcount.tv_sec = Startcount.tv_usec =0; Endcount.tv_sec = Endcount.tv_usec =0;#endifstopped =0; Starttimeinmicrosec =0; Endtimeinmicrosec =0;}/////////////////////////////////////////////////////////////////////////////////Distructor///////////////////////////////////////////////////////////////////////////////timer::~Timer () {}/////////////////////////////////////////////////////////////////////////////////Start timer.//Startcount'll is set at the this point.///////////////////////////////////////////////////////////////////////////////voidTimer::start () {stopped =0;//Reset Stop Flag#ifdef WIN32 QueryPerformanceCounter (&Startcount);#elseGettimeofday (&Startcount, NULL);#endif}/////////////////////////////////////////////////////////////////////////////////Stop the timer.//Endcount'll is set at the this point.///////////////////////////////////////////////////////////////////////////////voidTimer::stop () {stopped =1;//Set timer stopped flag#ifdef WIN32 QueryPerformanceCounter (&Endcount);#elseGettimeofday (&Endcount, NULL);#endif}/////////////////////////////////////////////////////////////////////////////////COMPUTE elapsed time in micro-second resolution.//Other GetElapsedTime would call the this first and then convert to correspond resolution.///////////////////////////////////////////////////////////////////////////////DoubleTimer::getelapsedtimeinmicrosec () {#ifdef WIN32if (!Stopped) QueryPerformanceCounter (&Endcount); Starttimeinmicrosec = Startcount.quadpart * (1000000.0/Frequency. QuadPart); Endtimeinmicrosec = Endcount.quadpart * (1000000.0/Frequency. QuadPart);#elseif (!Stopped) Gettimeofday (&Endcount, NULL); Starttimeinmicrosec = (STARTCOUNT.TV_SEC *1000000.0) +Startcount.tv_usec; Endtimeinmicrosec = (ENDCOUNT.TV_SEC *1000000.0) +Endcount.tv_usec;#endifReturn Endtimeinmicrosec-Starttimeinmicrosec;}/////////////////////////////////////////////////////////////////////////////////Divide elapsedtimeinmicrosec by 1000///////////////////////////////////////////////////////////////////////////////DoubleTimer::getelapsedtimeinmillisec () {ReturnThis->getelapsedtimeinmicrosec () *0.001;}/////////////////////////////////////////////////////////////////////////////////Divide elapsedtimeinmicrosec by 1000000///////////////////////////////////////////////////////////////////////////////double Timer::getelapsedtimeinsec () {return this->getelapsedtimeinmicrosec () * ///////////////////////////////////////////////////////////////////////////////// same as getelapsedtimeinsec () ///////////////////////////////////////////////////////////////////////////////< Span style= "color: #0000ff;" >double Timer::getelapsedtime () {return this->getelapsedtimeinsec ();}         

////////////////////////////////////////////////////////////////////////////////Timer.h//=======//High Resolution Timer.//This timer was able to measure the elapsed time with 1 micro-second accuracy//In both Windows, Linux and Unix system////Author:song Ho Ahn ([email protected])//Created:2003-01-13//Updated:2006-01-13////Copyright (c) 2003 Song Ho Ahn//////////////////////////////////////////////////////////////////////////////#ifndef Timer_h_def#define Timer_h_def#ifdef WIN32//Windows system Specific#include <windows.h>#else//Unix based System specific#include <sys/time.h>#endifClasstimer{Public: Timer ();//Default constructor ~timer ();//Default destructorvoid Start ();//Start timervoid Stop ();//Stop the timerDouble GetElapsedTime ();//Get elapsed time in secondDouble Getelapsedtimeinsec ();//Get elapsed time in second (same as GetElapsedTime)Double Getelapsedtimeinmillisec ();//Get elapsed time in Milli-secondDouble Getelapsedtimeinmicrosec ();//Get elapsed time in Micro-secondProtected:Private:Double starttimeinmicrosec;//Starting time in Micro-secondDouble endtimeinmicrosec;// Ending time in micro-second int stopped; // Stop flag #ifdef WIN32 large_integer frequency; // ticks per second large_integer Startcount; //Large_integer endcount; //#else timeval startcount; //Timeval endcount; //#endif};  #endif // timer_h_def              

Call Mode:

#include"Timer.h"Timer timer; //TimerDoubleElapsedTime;//Time in millisecondDoubleAvgtime =0;DoubleTestnum =0;voidTimebegin () {Timer.start ();}voidTimeend (stringstr) {ElapsedTime=timer.getelapsedtimeinmillisec (); Avgtime+=ElapsedTime; STR= str +"Time is :";//%lf ms\n ";cout << str << elapsedtime <<"Ms"<<Endl;}

Universal Time test functions under Windows and Linux platforms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.