[C++/C#] Implementation of timer Timer

1. Introduction

        The timer is implemented through several class libraries in C++ and C#, and the timer is used to count the time allowed by the program. In actual use, the millisecond timer is sufficient.

2. Millisecond timer

#include<ctime>
class Timer_milisecode{//
public:
    Timer_milisecode(){
        tbegin=clock();
        tend=clock();
    }

    void begin(){
        tbegin=clock();
    }
    void end(){
        tend=clock();
    }

    double spent(){
        return double(tend-tbegin)/CLOCKS_PER_SEC*1000;
    }

    double getCurrentTime(){
        return double(clock());
    }
private:    
    // const int clock_per_second=CLOCKS_PER_SEC; 
    clock_t tbegin,tend;
};

The header file is #include<ctime>, detailed explanation

        There is a clock_t type (long) under ctime, and a corresponding function clock(), whose return value is clock_t type, and there is a constant CLOCKS_PER_SEC, which represents the number of ticks per second. In theory, clock() should be Hit every millisecond. When calculating the time interval, subtract the two clock_t numbers, and then correct them to get the number of milliseconds.

3. Microsecond timer

#include<Windows.h>
class Timer_microseconds{
public:
    Timer_microseconds(){
        QueryPerformanceFrequency(&tc);
    }
    void begin(){
        QueryPerformanceCounter(&t1);
    }
    void end(){
        QueryPerformanceCounter(&t2);
    }
    double spent(){
        return  (double(t2.QuadPart-t1.QuadPart)*1000000.0)/tc.QuadPart;
    }

private:
    LARGE_INTEGER t1,t2,tc;

};

The header file is #include<windows.h>

        Similar to above. The first is to define three LARGE_INTEGER variables. There are two functions QueryPerformanceFrequency(), which queries the poor frequency per second, and QueryPerformanceCounter(), which is a counter. Then here, theoretically, the value of tc.QuadPart is 10_000_000 per second, that is, 10 will be counted every microsecond. This is more accurate.

4. Microsecond counter

#include<chrono>
class Timer_chrono{
public:
    Timer_chrono(){

    }

    void begin(){
        start=std::chrono::high_resolution_clock::now();
    }
    void end(){
        spentTime=std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now()-start).count();
    }
    double spent(){
        return (double)spentTime;
    }

private:
    std::chrono::time_point<std::chrono::high_resolution_clock> start;
    long long spentTime;
};

You need to use the header file <chrono>, which is like the above.

5. C# timer

    class Timer
    {
        private DateTime dt;
        private TimeSpan tSpen;
        public Timer()
        {
            dt= DateTime.Now;
        }

        public void begin()
        {
            dt= DateTime.Now;
        }
        public void end()
        {
            tSpen=DateTime.Now-dt;
        }
        public double spent()
        {
            return tSpen.TotalMilliseconds;
        }
    }

In fact, C# should not need any timers, just the DateTime and TimeSpan classes. The highest precision is milliseconds.

6. Get the current time in C++

    time_t tm0;
    time(&tm0);
    std::cout<<tm0<<endl;
    cout<<ctime(&tm0)<<endl;

The output is Mon Mar 20 23:29:15 2023. ctime is to format the seconds into a string

    tm ttm= *localtime(&tm0);
    cout<<ttm.tm_year<<"\t"<<ttm.tm_mon<<"\t"<<ttm.tm_mday<<endl;

struct tm, output the year and time since 1900.

Guess you like

Origin blog.csdn.net/weixin_43163656/article/details/129678302