1 /*
2  *                This source code is part of
3  *
4  *                     E  R  K  A  L  E
5  *                             -
6  *                       DFT from Hel
7  *
8  * Written by Susi Lehtola, 2010-2011
9  * Copyright (c) 2010-2011, Susi Lehtola
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  */
16 
17 
18 
19 #include <cmath>
20 #include <cstdio>
21 #include <string>
22 #include <sstream>
23 
24 #include "timer.h"
25 
26 #ifdef __MACH__
27 #include <mach/clock.h>
28 #include <mach/mach.h>
29 #endif
30 
Timer()31 Timer::Timer() {
32   set();
33 }
34 
~Timer()35 Timer::~Timer() {
36 }
37 
read(struct timespec * t) const38 void Timer::read(struct timespec *t) const {
39 #ifdef __MACH__
40   clock_serv_t cclock;
41   mach_timespec_t mts;
42   host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
43   clock_get_time(cclock, &mts);
44   mach_port_deallocate(mach_task_self(), cclock);
45   t->tv_sec = mts.tv_sec;
46   t->tv_nsec = mts.tv_nsec;
47 #else
48   clock_gettime(CLOCK_REALTIME,t);
49 #endif
50 }
51 
stop()52 void Timer::stop() {
53   struct timespec tstop;
54   read(&tstop);
55   elapsd+=(tstop.tv_sec-tstart.tv_sec)+(tstop.tv_nsec-tstart.tv_nsec)*1.0e-9;
56 }
57 
cont()58 void Timer::cont() {
59   read(&tstart);
60 }
61 
set()62 void Timer::set() {
63   // Get time.
64   read(&tstart);
65   elapsd=0.0;
66 }
67 
print() const68 void Timer::print() const {
69   printf("Time elapsed is %s.\n",elapsed().c_str());
70 }
71 
current_time() const72 std::string Timer::current_time() const {
73   char out[256];
74 
75   // Get time
76   time_t t;
77   time(&t);
78 
79   // Convert it into struct tm
80   struct tm tm;
81   gmtime_r(&t,&tm);
82 
83   const char * days[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
84   const char * months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
85 
86   // Print time
87   sprintf(out,"%s %02i %s %4i %02i:%02i:%02i",days[tm.tm_wday],tm.tm_mday,months[tm.tm_mon],1900+tm.tm_year,tm.tm_hour,tm.tm_min,tm.tm_sec);
88   return std::string(out);
89 }
90 
print_time() const91 void Timer::print_time() const {
92   printf("Current time is %s.\n",current_time().c_str());
93 }
94 
get() const95 double Timer::get() const {
96   struct timespec tstop;
97   read(&tstop);
98 
99   return elapsd+(tstop.tv_sec-tstart.tv_sec)+(tstop.tv_nsec-tstart.tv_nsec)*1.0e-9;
100 }
101 
elapsed() const102 std::string Timer::elapsed() const {
103   return parse(get());
104 }
105 
parse(double telapsed) const106 std::string Timer::parse(double telapsed) const {
107   std::ostringstream ret;
108 
109   // Minute is 60 sec
110   size_t min=60;
111   // Hour is 60 minutes
112   size_t hour=60*min;
113   // Day is 24 hours
114   size_t day=24*hour;
115 
116   // Compute number of days
117   size_t days=(size_t) trunc(telapsed/day);
118   if(days) {
119     telapsed-=days*day;
120 
121     ret << days << " d";
122   }
123 
124   // Compute number of hours
125   size_t hours=(size_t) trunc(telapsed/hour);
126   if(hours) {
127     telapsed-=hours*hour;
128 
129     // Check that there is a space at the end
130     std::string tmp=ret.str();
131     if(tmp.size() && tmp[tmp.size()-1]!=' ')
132       ret << " ";
133 
134     ret << hours << " h";
135   }
136 
137   // Compute number of minutes
138   size_t mins=(size_t) trunc(telapsed/min);
139   if(mins) {
140     telapsed-=mins*min;
141 
142     // Check that there is a space at the end
143     std::string tmp=ret.str();
144     if(tmp.size() && tmp[tmp.size()-1]!=' ')
145       ret << " ";
146 
147     ret << mins << " min";
148   }
149 
150   // Check that there is a space at the end
151   std::string tmp=ret.str();
152   if(tmp.size() && tmp[tmp.size()-1]!=' ')
153     ret << " ";
154 
155   char hlp[80];
156   sprintf(hlp,"%.2f s",telapsed);
157   ret << hlp;
158 
159   return ret.str();
160 }
161