chrono库功能挺强大的,但是太长了不方便记忆使用,写个简单的计时工具库。
libjc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #pragma once #ifndef LIBJC_H #define LIBJC_H #include <chrono> namespace libjc { class jctime_duration ; class jctime_high_resolution_clock { private : std::chrono::high_resolution_clock::time_point start_t ; std::chrono::high_resolution_clock::time_point stop_t ; unsigned char state; public : int start () ; int stop () ; libjc::jctime_duration duration () const ; libjc::jctime_duration operator -(const libjc::jctime_high_resolution_clock &) const ; jctime_high_resolution_clock (); }; class jctime_duration { friend libjc::jctime_duration libjc::jctime_high_resolution_clock::operator -(const libjc::jctime_high_resolution_clock &) const ; friend libjc::jctime_duration libjc::jctime_high_resolution_clock::duration () const ; private : std::chrono::nanoseconds rt; public : double tohour () const ; double tominute () const ; double tosecond () const ; double toms () const ; double tous () const ; long long int tons () const ; }; } #endif
CPP
libjc.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #include "libjc" namespace libjc { libjc::jctime_high_resolution_clock::jctime_high_resolution_clock () { this ->state = 0 ; } libjc::jctime_duration jctime_high_resolution_clock::operator -(const jctime_high_resolution_clock &t) const { jctime_duration a; a.rt = this ->start_t - t.start_t ; return a; } int libjc::jctime_high_resolution_clock::start () { if (state == 1 ) return 1 ; this ->start_t = std::chrono::high_resolution_clock::now (); state = 1 ; return 0 ; } int libjc::jctime_high_resolution_clock::stop () { this ->stop_t = std::chrono::high_resolution_clock::now (); state = 2 ; return 0 ; } libjc::jctime_duration libjc::jctime_high_resolution_clock::duration () const { libjc::jctime_duration a; a.rt = this ->stop_t - this ->start_t ; return a; } double libjc::jctime_duration::tohour () const { return std::chrono::duration<double , std::ratio<3600LL >>(rt).count (); } double libjc::jctime_duration::tominute () const { return std::chrono::duration<double , std::ratio<60LL >>(rt).count (); } double libjc::jctime_duration::tosecond () const { return std::chrono::duration <double >(rt).count (); } double libjc::jctime_duration::toms () const { return std::chrono::duration<double , std::ratio<1LL , 1000LL >>(rt).count (); } double libjc::jctime_duration::tous () const { return std::chrono::duration<double , std::ratio<1LL , 1000000LL >>(rt).count (); } long long int libjc::jctime_duration::tons () const { return rt.count (); } }
CPP
使用方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include <stdio.h> #include "libjc" int main () { libjc::jctime_high_resolution_clock tm1; tm1.start (); unsigned int a = ~(0 ); int b = 0 ; for (unsigned int i = 0 ; i < a; i++) { b++; } tm1.stop (); auto da = tm1.duration (); printf ("time is :\n %lf s\n %lf hour \n %lf minutes \n %lf ms \n %lf us \n %lld ns\n" , da.tosecond (), da.tohour (), da.tominute (), da.toms (), da.tous (), da.tons ()); return 0 ; }
CPP