Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:22

0001 #include <iostream>
0002 #include <iomanip>
0003 #include <vector>
0004 #include "stimer.h"
0005 
0006 #include "NP.hh"
0007 
0008 void test_start_stop()
0009 {
0010     stimer* t = new stimer  ; 
0011     t->start(); 
0012     stimer::sleep(1) ; 
0013     t->stop(); 
0014     double dt0 = t->duration(); 
0015     std::cout << " dt0 " << dt0 << std::endl ; 
0016 
0017     t->start(); 
0018     t->stop(); 
0019     double dt1 = t->duration(); 
0020 
0021     std::cout << " dt1 " << dt1 << std::endl ; 
0022 }
0023 
0024 void test_done()
0025 {
0026     stimer* t = stimer::create(); 
0027     stimer::sleep(1); 
0028     double dt0 = t->done(); 
0029     std::cout << " dt0 " << dt0 << std::endl ; 
0030     t->start(); 
0031     stimer::sleep(1); 
0032     double dt1 = t->done(); 
0033     std::cout << " dt1 " << dt1 << std::endl ; 
0034 }
0035 
0036 
0037 void test_lap()
0038 {
0039     stimer* t = stimer::create(); 
0040     stimer::sleep(1); 
0041     double dt0 = t->lap(); 
0042     std::cout << " dt0 " << dt0 << std::endl ; 
0043     stimer::sleep(1); 
0044     double dt1 = t->lap(); 
0045     std::cout << " dt1 " << dt1 << std::endl ; 
0046 }
0047 
0048 struct Egg
0049 {
0050    static stimer* TIMER ; 
0051    double boil(int seconds) ; 
0052 };
0053 
0054 stimer* Egg::TIMER = new stimer ; 
0055 
0056 inline double Egg::boil(int seconds)
0057 {
0058     TIMER->start()  ; 
0059     stimer::sleep(seconds); 
0060     return TIMER->done(); 
0061 }
0062 
0063 void test_egg()
0064 {
0065     Egg d ; 
0066 
0067     double dt0 = d.boil(1) ; 
0068     std::cout << " dt0 " << dt0 << std::endl ; 
0069 
0070     double dt1 = d.boil(1) ; 
0071     std::cout << " dt1 " << dt1 << std::endl ; 
0072 
0073     double dt2 = d.boil(0) ; 
0074     std::cout << " dt2 " << dt2 << std::endl ; 
0075 }
0076 
0077 void test_desc()
0078 {
0079     stimer* t = new stimer ; 
0080     std::cout << t->desc() << std::endl ; 
0081 
0082     t->start(); 
0083     std::cout << t->desc() << std::endl ; 
0084 
0085     stimer::sleep(1); 
0086 
0087     t->stop(); 
0088     std::cout << t->desc() << std::endl ; 
0089 
0090 }
0091 
0092 
0093 void test_convert_0()
0094 {
0095     // https://stackoverflow.com/questions/31255486/how-do-i-convert-a-stdchronotime-point-to-long-and-back
0096 
0097     using namespace std::chrono;
0098     auto now = system_clock::now();
0099     auto now_ms = time_point_cast<milliseconds>(now);
0100 
0101     auto value = now_ms.time_since_epoch();
0102     long duration = value.count();
0103 
0104     milliseconds dur(duration);
0105 
0106     time_point<system_clock> dt(dur);
0107 
0108     if (dt != now_ms)
0109         std::cout << "Failure." << std::endl;
0110     else
0111         std::cout << "Success." << std::endl;
0112 }
0113 
0114 
0115 void test_convert_1()
0116 {
0117     std::chrono::time_point<std::chrono::system_clock> t0 = std::chrono::system_clock::now();
0118 
0119     unsigned long long ns0 = t0.time_since_epoch().count();
0120     std::cout << " ns0 " << ns0 << std::endl ; 
0121 
0122     std::chrono::system_clock::duration d(ns0) ; 
0123     std::chrono::system_clock::time_point t1(d);
0124 
0125     unsigned long long ns1 = t1.time_since_epoch().count();
0126     std::cout << " ns1 " << ns1 << std::endl ; 
0127 
0128     std::chrono::duration<double> dt0 = t0.time_since_epoch() ; 
0129     std::chrono::duration<double> dt1 = t1.time_since_epoch() ; 
0130     double _dt0 =  dt0.count() ; 
0131     double _dt1 =  dt1.count() ; 
0132     
0133     std::cout << " _dt0 " << std::scientific << _dt0 << std::endl ; 
0134     std::cout << " _dt1 " << std::scientific << _dt1 << std::endl ; 
0135 }
0136 
0137 void test_convert_2()
0138 {
0139     // working out how to convert a time_point into POD and back 
0140 
0141     using clock = std::chrono::system_clock ; 
0142     //using clock = std::chrono::steady_clock ; 
0143     //using clock = std::chrono::high_resolution_clock ; 
0144 
0145     std::chrono::time_point<clock> t0 = clock::now();
0146     std::cout << " t0 : " << stimer::Format(t0) << std::endl ; 
0147 
0148     uint64_t ns0 = t0.time_since_epoch().count();
0149     std::cout << " ns0 " << ns0 << std::endl ; 
0150 
0151     clock::duration d(ns0) ; 
0152     clock::time_point t1(d);
0153 
0154     std::cout << " t1 : " << stimer::Format(t1) << std::endl ; 
0155 
0156 
0157     uint64_t ns1 = t1.time_since_epoch().count();
0158     std::cout << " ns1 " << ns1 << std::endl ; 
0159 
0160     std::chrono::duration<double> dt0 = t0.time_since_epoch() ; 
0161     std::chrono::duration<double> dt1 = t1.time_since_epoch() ; 
0162     double _dt0 =  dt0.count() ; 
0163     double _dt1 =  dt1.count() ; 
0164     
0165     std::cout << " _dt0 " << std::scientific << _dt0 << std::endl ; 
0166     std::cout << " _dt1 " << std::scientific << _dt1 << std::endl ; 
0167 }
0168 
0169 void test_convert_3()
0170 {
0171     stimer* tim = new stimer ; 
0172     tim->start(); 
0173 
0174     //using clock = std::chrono::high_resolution_clock ; 
0175     using clock = std::chrono::system_clock ; 
0176     uint64_t ec = tim->start_count(); 
0177     std::chrono::time_point<clock> t0 = stimer::TimePoint(ec) ; 
0178 
0179     std::cout 
0180         << " ec " << ec 
0181         << " t0 " << stimer::Format(t0)
0182         << " ecf " << stimer::Format(ec) 
0183         << std::endl
0184         ;
0185  
0186 }
0187 
0188 void test_TimePoint_0()
0189 {
0190     // hmm high_resolution_clock epochs dont travel between machines
0191     // try changing to system_clock epochs
0192 
0193     std::vector<uint64_t> ecs0 = { 1681415567555006257, 1681415570432562093, 1681415573040370404, 1681415575667773937, 1681415578398804926, 1681415581169340691, 1681415583816194221, 1681415586567097051, 1681415589753517102,
0194         1681415592426987792 } ; 
0195 
0196     std::vector<uint64_t> ecs1 = { 1681415825234793213, 1681415828042329959, 1681415831150709245, 1681415833581851217, 1681415836050784359, 1681415839417522516, 1681415842001793022, 1681415844497812015, 1681415846965829828,
0197         1681415850332486246 } ; 
0198 
0199     assert( ecs0.size() == ecs1.size() ); 
0200 
0201     for(int p=0 ; p < 2 ; p++)
0202     {
0203     for(int i=0 ; i < int(ecs0.size()) ; i++ )
0204     {
0205          uint64_t ec0 = ecs0[i] ; 
0206          uint64_t ec1 = ecs1[i] ; 
0207 
0208          if( p == 0 ) std::cout 
0209              << " ec0 " << std::setw(20) << ec0 
0210              << " ec1 " << std::setw(20) << ec1
0211              << std::endl 
0212              ;
0213 
0214          if( p == 1 ) std::cout 
0215              << " ec0 " << stimer::Format(ec0) 
0216              << " ec1 " << stimer::Format(ec1)
0217              << std::endl 
0218              ;
0219     }
0220     }
0221 
0222 }
0223 
0224 
0225 /**
0226 
0227 
0228 Previously has to divide the Linux epoch count 
0229 by 1000 to be comparable with the Darwin one.
0230 
0231 Instead of such a hack, changed EpochCount to 
0232 standardize on microsecond epoch counts. 
0233 This makes the time stamps comparable modulo 7 hours time difference. 
0234 
0235 N[blyth@localhost tests]$ ./stimer_test.sh 
0236  stimer::Format(1681469652582925) : Fri, 14.04.2023 18:54:12
0237  stimer::Format(1681467459953485) : Fri, 14.04.2023 18:17:39
0238  stimer::Format(1681467438445254) : Fri, 14.04.2023 18:17:18
0239 N[blyth@localhost tests]$ date
0240 Fri Apr 14 18:54:19 CST 2023
0241 N[blyth@localhost tests]$ 
0242 
0243 epsilon:tests blyth$ ./stimer_test.sh 
0244  stimer::Format(1681469695498380) : Fri, 14.04.2023 11:54:55
0245  stimer::Format(1681467459953485) : Fri, 14.04.2023 11:17:39
0246  stimer::Format(1681467438445254) : Fri, 14.04.2023 11:17:18
0247 epsilon:tests blyth$ date
0248 Fri Apr 14 11:55:00 BST 2023
0249 epsilon:tests blyth$ 
0250 
0251 **/
0252 
0253 
0254 void test_EpochCountNow_0()
0255 {
0256     std::vector<uint64_t> ecns = { stimer::EpochCountNow(), 1681467459953485, 1681467438445254316/1000 } ; 
0257 
0258     for(int i=0 ; i < int(ecns.size()) ; i++) 
0259     {
0260         uint64_t ecn = ecns[i] ; 
0261         std::cout 
0262             << " stimer::Format(" << ecn << ") : " << stimer::Format(ecn) 
0263             << std::endl 
0264             ; 
0265     }
0266 
0267 }
0268 
0269 void test_EpochCountNow_1()
0270 {
0271     static const int N = 100000 ; 
0272     NP* t = NP::Make<uint64_t>(N) ; 
0273     uint64_t* tt = t->values<uint64_t>() ; 
0274     for(int i=0 ; i < N ; i++) tt[i] = stimer::EpochCountNow() ;   
0275     t->save("$TTPATH"); 
0276 }
0277 
0278 
0279 
0280 void test_count()
0281 {
0282     stimer* tim = stimer::create() ;
0283 
0284     stimer::sleep(1); 
0285 
0286     double dur = tim->done(); 
0287 
0288     std::cout << tim->desc() << std::endl ; 
0289 
0290     uint64_t t0 = tim->start_count(); 
0291     uint64_t t1 = tim->stop_count(); 
0292     std::cout 
0293         << " stimer::Format(t0) " << stimer::Format(t0) << " t0 " << t0 << std::endl 
0294         << " stimer::Format(t1) " << stimer::Format(t1) << " t1 " << t1 << std::endl  
0295         ; 
0296 
0297 
0298     NP* t = NP::Make<uint64_t>(2) ; 
0299     uint64_t* tt = t->values<uint64_t>() ; 
0300     tt[0] = t0 ; 
0301     tt[1] = t1 ; 
0302     t->save("$TTPATH"); 
0303 }
0304 
0305 /**
0306 From BST (UTC+1) timezone the numpy presented times are corrected to UTC::
0307 
0308     epsilon:tests blyth$ ./stimer_test.sh 
0309     stimer::desc status STOPPED _start 1681480063800788 start Fri, 14.04.2023 14:47:43 _stop 1681480064801308 stop Fri, 14.04.2023 14:47:44 duration 1.000520e+00
0310      stimer::Format(t0) Fri, 14.04.2023 14:47:43 t0 1681480063800788
0311      stimer::Format(t1) Fri, 14.04.2023 14:47:44 t1 1681480064801308
0312     [1681480063800788 1681480064801308]
0313 
0314     np.c_[tt.view('datetime64[us]')]
0315 
0316     [['2023-04-14T13:47:43.800788']
0317      ['2023-04-14T13:47:44.801308']]
0318 
0319 From CST (UTC+8) timezone the numpy presented times also corrected to UTC::
0320 
0321     N[blyth@localhost tests]$ ./stimer_test.sh 
0322     stimer::desc status STOPPED _start 1681480305503105 start Fri, 14.04.2023 21:51:45 _stop 1681480306505512 stop Fri, 14.04.2023 21:51:46 duration 1.002406e+00
0323      stimer::Format(t0) Fri, 14.04.2023 21:51:45 t0 1681480305503105
0324      stimer::Format(t1) Fri, 14.04.2023 21:51:46 t1 1681480306505512
0325     Python 3.7.7 (default, May  7 2020, 21:25:33) 
0326     Type 'copyright', 'credits' or 'license' for more information
0327     IPython 7.18.1 -- An enhanced Interactive Python. Type '?' for help.
0328     [1681480305503105 1681480306505512]
0329 
0330     np.c_[tt.view('datetime64[us]')]
0331 
0332     [['2023-04-14T13:51:45.503105']
0333      ['2023-04-14T13:51:46.505512']]
0334 
0335 **/
0336 
0337 
0338 
0339 int main()
0340 {
0341     /*
0342     test_start_stop(); 
0343     test_done(); 
0344     test_lap(); 
0345     test_egg(); 
0346     test_desc(); 
0347     test_convert_0(); 
0348     test_convert_1(); 
0349     test_convert_2(); 
0350     test_convert_3(); 
0351     test_TimePoint_0();
0352     test_EpochCountNow_0(); 
0353     test_count(); 
0354     */
0355 
0356     test_EpochCountNow_1(); 
0357 
0358     return 0 ; 
0359 }