source: src/Helpers/Chronos.cpp@ 8e24ef

Last change on this file since 8e24ef was 8e24ef, checked in by Frederik Heber <heber@…>, 15 years ago

Chronos is so far fully working.

  • Library version is now 7:0:0, API version is 1.1.0.
  • using either time.h or sys/times.h if present to measure timing.
  • Property mode set to 100644
File size: 3.2 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 "MemDebug.hpp"
21
22#include <iostream>
23
24#ifdef HAVE_SYS_TIMES_H
25# include <sys/times.h>
26#else
27# include <time.h>
28#endif
29
30#include "Chronos.hpp"
31
32#include "Singleton_impl.hpp"
33
34Chronos::Chronos()
35{}
36
37Chronos::~Chronos()
38{}
39
40double Chronos::getTime(const std::string _name) const
41{
42 // only those functions have a time that have run already
43 if (TimeRunning.count(_name)) {
44 // return -1 if function is currently running
45 if (TimeRunning.count(_name) != 0.)
46 return AccountedTime.at(_name);
47 else
48 return -1.;
49 }
50 return 0.;
51}
52
53void Chronos::resetTime(const std::string _name)
54{
55 if (TimeRunning.count(_name)) {
56 AccountedTime[_name] = 0.;
57 }
58}
59
60void Chronos::startTiming(const std::string _name)
61{
62 // start time keeping
63 TimeRunning[_name] = getCurrentTime();
64}
65
66double Chronos::getCurrentTime() const
67{
68#ifdef HAVE_SYS_TIMES_H
69 struct tms *buffer = new tms;
70 double currenttime;
71 if (times(buffer) != (clock_t)(-1))
72 currenttime = ((double)buffer->tms_utime/(double)CLOCKS_PER_SEC);
73 else
74 currenttime = 0.;
75 delete buffer;
76#else
77 const double currenttime = (clock()/(double)CLOCKS_PER_SEC);
78#endif
79 //std::cout << "Current time is " << currenttime << std::endl;
80 return currenttime;
81}
82
83void Chronos::endTiming(const std::string _name)
84{
85 const double endtime = getCurrentTime();
86 const double starttime = TimeRunning[_name];
87
88 // if present
89 ASSERT(TimeRunning.count(_name), "Chronos::endTiming() - no timer under "+_name+" running.");
90 // finish time keeping
91 const double RunTime = ((double)endtime - starttime);
92 TimekeepingMap::iterator iter = AccountedTime.find(_name);
93 if (iter != AccountedTime.end())
94 AccountedTime[_name] += RunTime;
95 else
96 AccountedTime[_name] = RunTime;
97
98 // and zero for next run
99 TimeRunning[_name] = 0.;
100}
101
102double Chronos::SumUpTotalTime() const
103{
104 double sum = 0.;
105 for (TimekeepingMap::const_iterator iter = AccountedTime.begin();
106 iter != AccountedTime.end();
107 ++iter) {
108 sum += iter->second;
109 }
110 return sum;
111}
112
113size_t Chronos::SumUpTotalFunctions() const
114{
115 return TimeRunning.size();
116}
117
118std::ostream& operator<<(std::ostream &ost, const Chronos &_time)
119{
120 ost << "List of functions present:" << std::endl;
121 for (Chronos::TimekeepingMap::const_iterator iter = _time.AccountedTime.begin();
122 iter != _time.AccountedTime.end();
123 ++iter)
124 ost << "\t" << iter->first << "\t" << iter->second << "s" << std::endl;
125 ost << "Total time passed: " << _time.SumUpTotalTime() << std::endl;
126 ost << "Total functions: " << _time.SumUpTotalFunctions() << std::endl;
127 return ost;
128}
129
130// construct the remainder of the singleton
131CONSTRUCT_SINGLETON(Chronos)
132
133// catch if someone wants to use Info objects in here
134#ifdef INFO_HPP_
135BOOST_PP_ASSERT_MSG(1,\
136 ERROR: This is a safety measure to generate a compiler warning\n \
137 if you really try to use info.hpp in __FILE__.)
138#endif
139
Note: See TracBrowser for help on using the repository browser.