| [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 |  | 
|---|
|  | 20 | #include "MemDebug.hpp" | 
|---|
|  | 21 |  | 
|---|
|  | 22 | #include <iostream> | 
|---|
|  | 23 |  | 
|---|
| [8e24ef] | 24 | #ifdef HAVE_SYS_TIMES_H | 
|---|
|  | 25 | # include <sys/times.h> | 
|---|
|  | 26 | #else | 
|---|
|  | 27 | # include <time.h> | 
|---|
|  | 28 | #endif | 
|---|
|  | 29 |  | 
|---|
| [93abe8] | 30 | #include "Chronos.hpp" | 
|---|
|  | 31 |  | 
|---|
|  | 32 | #include "Singleton_impl.hpp" | 
|---|
|  | 33 |  | 
|---|
|  | 34 | Chronos::Chronos() | 
|---|
|  | 35 | {} | 
|---|
|  | 36 |  | 
|---|
|  | 37 | Chronos::~Chronos() | 
|---|
|  | 38 | {} | 
|---|
|  | 39 |  | 
|---|
| [8e24ef] | 40 | double Chronos::getTime(const std::string _name) const | 
|---|
| [93abe8] | 41 | { | 
|---|
|  | 42 | // only those functions have a time that have run already | 
|---|
| [8e24ef] | 43 | if (TimeRunning.count(_name)) { | 
|---|
| [93abe8] | 44 | // return -1 if function is currently running | 
|---|
| [8e24ef] | 45 | if (TimeRunning.count(_name) != 0.) | 
|---|
| [93abe8] | 46 | return AccountedTime.at(_name); | 
|---|
|  | 47 | else | 
|---|
| [8e24ef] | 48 | return -1.; | 
|---|
| [93abe8] | 49 | } | 
|---|
| [8e24ef] | 50 | return 0.; | 
|---|
| [93abe8] | 51 | } | 
|---|
|  | 52 |  | 
|---|
|  | 53 | void Chronos::resetTime(const std::string _name) | 
|---|
|  | 54 | { | 
|---|
| [8e24ef] | 55 | if (TimeRunning.count(_name)) { | 
|---|
|  | 56 | AccountedTime[_name] = 0.; | 
|---|
| [93abe8] | 57 | } | 
|---|
|  | 58 | } | 
|---|
|  | 59 |  | 
|---|
|  | 60 | void Chronos::startTiming(const std::string _name) | 
|---|
|  | 61 | { | 
|---|
|  | 62 | // start time keeping | 
|---|
| [8e24ef] | 63 | TimeRunning[_name] = getCurrentTime(); | 
|---|
|  | 64 | } | 
|---|
|  | 65 |  | 
|---|
|  | 66 | double 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; | 
|---|
| [93abe8] | 81 | } | 
|---|
|  | 82 |  | 
|---|
|  | 83 | void Chronos::endTiming(const std::string _name) | 
|---|
|  | 84 | { | 
|---|
| [8e24ef] | 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.; | 
|---|
| [93abe8] | 100 | } | 
|---|
|  | 101 |  | 
|---|
| [8e24ef] | 102 | double Chronos::SumUpTotalTime() const | 
|---|
| [93abe8] | 103 | { | 
|---|
| [8e24ef] | 104 | double sum = 0.; | 
|---|
| [93abe8] | 105 | for (TimekeepingMap::const_iterator iter = AccountedTime.begin(); | 
|---|
|  | 106 | iter != AccountedTime.end(); | 
|---|
|  | 107 | ++iter) { | 
|---|
|  | 108 | sum += iter->second; | 
|---|
|  | 109 | } | 
|---|
|  | 110 | return sum; | 
|---|
|  | 111 | } | 
|---|
|  | 112 |  | 
|---|
|  | 113 | size_t Chronos::SumUpTotalFunctions() const | 
|---|
|  | 114 | { | 
|---|
| [8e24ef] | 115 | return TimeRunning.size(); | 
|---|
| [93abe8] | 116 | } | 
|---|
|  | 117 |  | 
|---|
|  | 118 | std::ostream& operator<<(std::ostream &ost, const Chronos &_time) | 
|---|
|  | 119 | { | 
|---|
| [8e24ef] | 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; | 
|---|
| [93abe8] | 126 | ost << "Total functions: " << _time.SumUpTotalFunctions() << std::endl; | 
|---|
|  | 127 | return ost; | 
|---|
|  | 128 | } | 
|---|
|  | 129 |  | 
|---|
| [8e24ef] | 130 | // construct the remainder of the singleton | 
|---|
| [93abe8] | 131 | CONSTRUCT_SINGLETON(Chronos) | 
|---|
| [8e24ef] | 132 |  | 
|---|
|  | 133 | // catch if someone wants to use Info objects in here | 
|---|
|  | 134 | #ifdef INFO_HPP_ | 
|---|
|  | 135 | BOOST_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 |  | 
|---|