| [93abe8] | 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 |
|
|---|
| [9b8fa4] | 20 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| [93abe8] | 21 |
|
|---|
| 22 | #include <iostream>
|
|---|
| 23 |
|
|---|
| [760f97c] | 24 | #ifdef HAVE_UNISTD_H
|
|---|
| 25 | #include <unistd.h>
|
|---|
| [8e24ef] | 26 | #ifdef HAVE_SYS_TIMES_H
|
|---|
| 27 | # include <sys/times.h>
|
|---|
| 28 | #else
|
|---|
| 29 | # include <time.h>
|
|---|
| 30 | #endif
|
|---|
| [760f97c] | 31 | #else
|
|---|
| 32 | # include <time.h>
|
|---|
| 33 | #endif
|
|---|
| 34 |
|
|---|
| [8e24ef] | 35 |
|
|---|
| [9b8fa4] | 36 | #include "CodePatterns/Chronos.hpp"
|
|---|
| [93abe8] | 37 |
|
|---|
| [9b8fa4] | 38 | #include "CodePatterns/Singleton_impl.hpp"
|
|---|
| [93abe8] | 39 |
|
|---|
| 40 | Chronos::Chronos()
|
|---|
| 41 | {}
|
|---|
| 42 |
|
|---|
| 43 | Chronos::~Chronos()
|
|---|
| 44 | {}
|
|---|
| 45 |
|
|---|
| [8e24ef] | 46 | double Chronos::getTime(const std::string _name) const
|
|---|
| [93abe8] | 47 | {
|
|---|
| 48 | // only those functions have a time that have run already
|
|---|
| [8e24ef] | 49 | if (TimeRunning.count(_name)) {
|
|---|
| [93abe8] | 50 | // return -1 if function is currently running
|
|---|
| [8e24ef] | 51 | if (TimeRunning.count(_name) != 0.)
|
|---|
| [93abe8] | 52 | return AccountedTime.at(_name);
|
|---|
| 53 | else
|
|---|
| [8e24ef] | 54 | return -1.;
|
|---|
| [93abe8] | 55 | }
|
|---|
| [8e24ef] | 56 | return 0.;
|
|---|
| [93abe8] | 57 | }
|
|---|
| 58 |
|
|---|
| 59 | void Chronos::resetTime(const std::string _name)
|
|---|
| 60 | {
|
|---|
| [8e24ef] | 61 | if (TimeRunning.count(_name)) {
|
|---|
| 62 | AccountedTime[_name] = 0.;
|
|---|
| [93abe8] | 63 | }
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | void Chronos::startTiming(const std::string _name)
|
|---|
| 67 | {
|
|---|
| 68 | // start time keeping
|
|---|
| [8e24ef] | 69 | TimeRunning[_name] = getCurrentTime();
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | double 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))
|
|---|
| [760f97c] | 78 | currenttime = ((double)buffer->tms_utime/(double)sysconf(_SC_CLK_TCK));
|
|---|
| [8e24ef] | 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;
|
|---|
| [93abe8] | 87 | }
|
|---|
| 88 |
|
|---|
| 89 | void Chronos::endTiming(const std::string _name)
|
|---|
| 90 | {
|
|---|
| [8e24ef] | 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.;
|
|---|
| [93abe8] | 106 | }
|
|---|
| 107 |
|
|---|
| [8e24ef] | 108 | double Chronos::SumUpTotalTime() const
|
|---|
| [93abe8] | 109 | {
|
|---|
| [8e24ef] | 110 | double sum = 0.;
|
|---|
| [93abe8] | 111 | for (TimekeepingMap::const_iterator iter = AccountedTime.begin();
|
|---|
| 112 | iter != AccountedTime.end();
|
|---|
| 113 | ++iter) {
|
|---|
| 114 | sum += iter->second;
|
|---|
| 115 | }
|
|---|
| 116 | return sum;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | size_t Chronos::SumUpTotalFunctions() const
|
|---|
| 120 | {
|
|---|
| [8e24ef] | 121 | return TimeRunning.size();
|
|---|
| [93abe8] | 122 | }
|
|---|
| 123 |
|
|---|
| 124 | std::ostream& operator<<(std::ostream &ost, const Chronos &_time)
|
|---|
| 125 | {
|
|---|
| [8e24ef] | 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;
|
|---|
| [93abe8] | 132 | ost << "Total functions: " << _time.SumUpTotalFunctions() << std::endl;
|
|---|
| 133 | return ost;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| [8e24ef] | 136 | // construct the remainder of the singleton
|
|---|
| [93abe8] | 137 | CONSTRUCT_SINGLETON(Chronos)
|
|---|
| [8e24ef] | 138 |
|
|---|
| 139 | // catch if someone wants to use Info objects in here
|
|---|
| 140 | #ifdef INFO_HPP_
|
|---|
| 141 | BOOST_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 |
|
|---|