![]() |
Shadowrun: Awakened 29 September 2011 - Build 871
|
00001 #include "Stopwatch.h" 00002 00003 namespace Support 00004 { 00005 ManagedDictionary<std::string, StopwatchAccumulator> Stopwatch::_Accumulators(true); 00006 boost::mutex Stopwatch::_AccumulatorsMutex; 00007 00008 void Stopwatch::AccumulateTime(const std::string& name, double timeElapsed) 00009 { 00010 //lock so only one thread can manipulator _Accumulators at a time 00011 boost::lock_guard<boost::mutex> guard(_AccumulatorsMutex); 00012 00013 //save the time amongst the accumulators 00014 StopwatchAccumulator* accumulator = _Accumulators.getSafe(name); 00015 if(accumulator == NULL) 00016 { 00017 //make a new accumulator and save it 00018 //its memory will be deallocated when the managed dictionary leaves scope 00019 accumulator = new StopwatchAccumulator(); 00020 (*accumulator)(timeElapsed); 00021 _Accumulators[name] = accumulator; 00022 } 00023 else 00024 (*accumulator)(timeElapsed); 00025 } 00026 00030 void Stopwatch::PrintAccumulatedTimes() 00031 { 00032 ManagedDictionary<std::string, StopwatchAccumulator>::const_iterator iter; 00033 ManagedDictionary<std::string, StopwatchAccumulator>::const_iterator end; 00034 00035 printf("\r\n Count| Min| Max| Mean| Total\r\n"); 00036 for(iter = _Accumulators.begin(), end = _Accumulators.end(); iter != end; ++iter) 00037 { 00038 StopwatchAccumulator* curAcc = iter->second; 00039 printf("%s:\r\n%10d| %6.4g| %6.4g| %6.4g| %6.4g\r\n", iter->first.data(), 00040 boost::accumulators::extract::count(*curAcc), 00041 (boost::accumulators::extract::min)(*curAcc), 00042 (boost::accumulators::extract::max)(*curAcc), 00043 boost::accumulators::extract::mean(*curAcc), 00044 boost::accumulators::extract::sum(*curAcc)); 00045 } 00046 } 00047 00052 Stopwatch::Stopwatch(const std::string& name) : _name(name), _timeAtConstruction(tbb::tick_count::now()) 00053 { 00054 } 00055 00061 Stopwatch::~Stopwatch() 00062 { 00063 tbb::tick_count t1 = tbb::tick_count::now(); 00064 double seconds = (t1-_timeAtConstruction).seconds(); 00065 AccumulateTime(_name, seconds); 00066 } 00067 }
Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.