|
| 1 | +/* |
| 2 | + * Cppcheck - A tool for static C/C++ code analysis |
| 3 | + * Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +#include <iostream> |
| 19 | +#include "timer.h" |
| 20 | + |
| 21 | +/* |
| 22 | + TODO: |
| 23 | + - handle SHOWTIME_TOP5 in TimerResults |
| 24 | + - sort list by time |
| 25 | + - do not sort the results alphabetically |
| 26 | + - rename "file" to "single" |
| 27 | + - synchronise map access in multithreaded mode or disable timing |
| 28 | + - add unit tests |
| 29 | + - for --showtime (needs input file) |
| 30 | + - for Timer* classes |
| 31 | +*/ |
| 32 | + |
| 33 | + |
| 34 | +void TimerResults::ShowResults() |
| 35 | +{ |
| 36 | + std::clock_t overallClocks = 0; |
| 37 | + |
| 38 | + std::map<std::string, struct TimerResultsData>::const_iterator I = _results.begin(); |
| 39 | + const std::map<std::string, struct TimerResultsData>::const_iterator E = _results.end(); |
| 40 | + |
| 41 | + while (I != E) |
| 42 | + { |
| 43 | + const double sec = (double)I->second._clocks / CLOCKS_PER_SEC; |
| 44 | + const double secAverage = (double)(I->second._clocks / I->second._numberOfResults) / CLOCKS_PER_SEC; |
| 45 | + std::cout << I->first << ": " << sec << "s (avg. " << secAverage << "s - " << I->second._numberOfResults << " result(s))" << std::endl; |
| 46 | + |
| 47 | + overallClocks += I->second._clocks; |
| 48 | + |
| 49 | + ++I; |
| 50 | + } |
| 51 | + |
| 52 | + const double secOverall = (double)overallClocks / CLOCKS_PER_SEC; |
| 53 | + std::cout << "Overall time: " << secOverall << "s" << std::endl; |
| 54 | +} |
| 55 | + |
| 56 | +void TimerResults::AddResults(const std::string& str, std::clock_t clocks) |
| 57 | +{ |
| 58 | + _results[str]._clocks += clocks; |
| 59 | + _results[str]._numberOfResults++; |
| 60 | +} |
| 61 | + |
| 62 | +Timer::Timer(const std::string& str, unsigned int showtimeMode, TimerResultsIntf* timerResults) |
| 63 | + : _str(str) |
| 64 | + , _showtimeMode(showtimeMode) |
| 65 | + , _start(0) |
| 66 | + , _stopped(false) |
| 67 | + , _timerResults(timerResults) |
| 68 | +{ |
| 69 | + if (showtimeMode != SHOWTIME_NONE) |
| 70 | + _start = std::clock(); |
| 71 | +} |
| 72 | + |
| 73 | +Timer::~Timer() |
| 74 | +{ |
| 75 | + Stop(); |
| 76 | +} |
| 77 | + |
| 78 | +void Timer::Stop() |
| 79 | +{ |
| 80 | + if ((_showtimeMode != SHOWTIME_NONE) && !_stopped) |
| 81 | + { |
| 82 | + const std::clock_t end = std::clock(); |
| 83 | + const std::clock_t diff = end - _start; |
| 84 | + |
| 85 | + if (_showtimeMode == SHOWTIME_FILE) |
| 86 | + { |
| 87 | + double sec = (double)diff / CLOCKS_PER_SEC; |
| 88 | + std::cout << _str << ": " << sec << "s" << std::endl; |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + if (_timerResults) |
| 93 | + _timerResults->AddResults(_str, diff); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + _stopped = true; |
| 98 | +} |
0 commit comments