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
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
0140
0141 using clock = std::chrono::system_clock ;
0142
0143
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
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
0191
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
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
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
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339 int main()
0340 {
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356 test_EpochCountNow_1();
0357
0358 return 0 ;
0359 }