source: src/Helpers/Chronos.cpp@ 760f97c

Last change on this file since 760f97c was 760f97c, checked in by Frederik Heber <heber@…>, 14 years ago

FIX: In case of sys/times.h presence we used wrong clocks_to_sec conversion.

  • correct one is via sysconf(_SC_CLK_TCK).
  • bug reported by Jan Hamaekers.
  • Property mode set to 100644
File size: 3.3 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * Chronos.cpp
10 *
11 * Created on: Mar 14, 2011
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "CodePatterns/MemDebug.hpp"
21
22#include <iostream>
23
24#ifdef HAVE_UNISTD_H
25#include <unistd.h>
26#ifdef HAVE_SYS_TIMES_H
27# include <sys/times.h>
28#else
29# include <time.h>
30#endif
31#else
32# include <time.h>
33#endif
34
35
36#include "CodePatterns/Chronos.hpp"
37
38#include "CodePatterns/Singleton_impl.hpp"
39
40Chronos::Chronos()
41{}
42
43Chronos::~Chronos()
44{}
45
46double Chronos::getTime(const std::string _name) const
47{
48 // only those functions have a time that have run already
49 if (TimeRunning.count(_name)) {
50 // return -1 if function is currently running
51 if (TimeRunning.count(_name) != 0.)
52 return AccountedTime.at(_name);
53 else
54 return -1.;
55 }
56 return 0.;
57}
58
59void Chronos::resetTime(const std::string _name)
60{
61 if (TimeRunning.count(_name)) {
62 AccountedTime[_name] = 0.;
63 }
64}
65
66void Chronos::startTiming(const std::string _name)
67{
68 // start time keeping
69 TimeRunning[_name] = getCurrentTime();
70}
71
72double Chronos::getCurrentTime() const
73{
74#ifdef HAVE_SYS_TIMES_H
75 struct tms *buffer = new tms;
76 double currenttime;
77 if (times(buffer) != (clock_t)(-1))
78 currenttime = ((double)buffer->tms_utime/(double)sysconf(_SC_CLK_TCK));
79 else
80 currenttime = 0.;
81 delete buffer;
82#else
83 const double currenttime = (clock()/(double)CLOCKS_PER_SEC);
84#endif
85 //std::cout << "Current time is " << currenttime << std::endl;
86 return currenttime;
87}
88
89void Chronos::endTiming(const std::string _name)
90{
91 const double endtime = getCurrentTime();
92 const double starttime = TimeRunning[_name];
93
94 // if present
95 ASSERT(TimeRunning.count(_name), "Chronos::endTiming() - no timer under "+_name+" running.");
96 // finish time keeping
97 const double RunTime = ((double)endtime - starttime);
98 TimekeepingMap::iterator iter = AccountedTime.find(_name);
99 if (iter != AccountedTime.end())
100 AccountedTime[_name] += RunTime;
101 else
102 AccountedTime[_name] = RunTime;
103
104 // and zero for next run
105 TimeRunning[_name] = 0.;
106}
107
108double Chronos::SumUpTotalTime() const
109{
110 double sum = 0.;
111 for (TimekeepingMap::const_iterator iter = AccountedTime.begin();
112 iter != AccountedTime.end();
113 ++iter) {
114 sum += iter->second;
115 }
116 return sum;
117}
118
119size_t Chronos::SumUpTotalFunctions() const
120{
121 return TimeRunning.size();
122}
123
124std::ostream& operator<<(std::ostream &ost, const Chronos &_time)
125{
126 ost << "List of functions present:" << std::endl;
127 for (Chronos::TimekeepingMap::const_iterator iter = _time.AccountedTime.begin();
128 iter != _time.AccountedTime.end();
129 ++iter)
130 ost << "\t" << iter->first << "\t" << iter->second << "s" << std::endl;
131 ost << "Total time passed: " << _time.SumUpTotalTime() << std::endl;
132 ost << "Total functions: " << _time.SumUpTotalFunctions() << std::endl;
133 return ost;
134}
135
136// construct the remainder of the singleton
137CONSTRUCT_SINGLETON(Chronos)
138
139// catch if someone wants to use Info objects in here
140#ifdef INFO_HPP_
141BOOST_PP_ASSERT_MSG(1,\
142 ERROR: This is a safety measure to generate a compiler warning\n \
143 if you really try to use info.hpp in __FILE__.)
144#endif
145
Note: See TracBrowser for help on using the repository browser.