diff options
author | Tim Mayberry <mojofunk@gmail.com> | 2014-12-22 12:39:57 +0700 |
---|---|---|
committer | Tim Mayberry <mojofunk@gmail.com> | 2015-01-01 18:58:57 +0700 |
commit | 770c190ccff8e6096e0bff9c78b42633020f72be (patch) | |
tree | 2624277820b0d21bd70a8ce4c843c90751df5147 /libs/pbd/timing.cc | |
parent | feb701ceec92d4fc772509d13d62627caacaad83 (diff) |
Add Timing, TimingData and Timed classes for time measurement to pbd/timing.h
Diffstat (limited to 'libs/pbd/timing.cc')
-rw-r--r-- | libs/pbd/timing.cc | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/libs/pbd/timing.cc b/libs/pbd/timing.cc new file mode 100644 index 0000000000..b811c09f96 --- /dev/null +++ b/libs/pbd/timing.cc @@ -0,0 +1,66 @@ +/* + Copyright (C) 2014 Tim Mayberry + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include "pbd/timing.h" + +#include <sstream> +#include <limits> + +namespace PBD { + +bool +get_min_max_avg_total (const std::vector<uint64_t>& values, uint64_t& min, uint64_t& max, uint64_t& avg, uint64_t& total) +{ + if (values.empty()) { + return false; + } + + total = 0; + min = std::numeric_limits<uint64_t>::max(); + max = 0; avg = 0; + + for (std::vector<uint64_t>::const_iterator ci = values.begin(); ci != values.end(); ++ci) { + total += *ci; + min = std::min (min, *ci); + max = std::max (max, *ci); + } + + avg = total / values.size(); + return true; +} + +std::string +timing_summary (const std::vector<uint64_t>& values) +{ + std::ostringstream oss; + + uint64_t min, max, avg, total; + + if (get_min_max_avg_total (values, min, max, avg, total)) { + oss << "Count: " << values.size() + << " Min: " << min + << " Max: " << max + << " Avg: " << avg + << " Total: " << total + << std::endl; + } + return oss.str(); +} + +} // namespace PBD |