💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.5 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "performance-sampler.hpp"
  20. #include <algorithm>
  21. #include <numeric>
  22. performance_sampler::performance_sampler():
  23. sample_size(1),
  24. sample_index(0)
  25. {}
  26. void performance_sampler::sample(double duration)
  27. {
  28. if (samples.size() < sample_size)
  29. {
  30. samples.push_back(duration);
  31. }
  32. else
  33. {
  34. samples[sample_index] = duration;
  35. sample_index = (sample_index + 1) % samples.size();
  36. }
  37. }
  38. void performance_sampler::reset()
  39. {
  40. samples.clear();
  41. sample_index = 0;
  42. }
  43. void performance_sampler::set_sample_size(std::size_t size)
  44. {
  45. sample_size = size;
  46. if (samples.size() > size)
  47. {
  48. samples.resize(size);
  49. sample_index = 0;
  50. }
  51. }
  52. double performance_sampler::mean_frame_duration() const
  53. {
  54. return std::accumulate(samples.begin(), samples.end(), 0.0) / static_cast<double>(samples.size());
  55. }