💿🐜 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.

66 lines
1.6 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. namespace debug {
  23. performance_sampler::performance_sampler():
  24. sample_size(1),
  25. sample_index(0)
  26. {}
  27. void performance_sampler::sample(double duration)
  28. {
  29. if (samples.size() < sample_size)
  30. {
  31. samples.push_back(duration);
  32. }
  33. else
  34. {
  35. samples[sample_index] = duration;
  36. sample_index = (sample_index + 1) % samples.size();
  37. }
  38. }
  39. void performance_sampler::reset()
  40. {
  41. samples.clear();
  42. sample_index = 0;
  43. }
  44. void performance_sampler::set_sample_size(std::size_t size)
  45. {
  46. sample_size = size;
  47. if (samples.size() > size)
  48. {
  49. samples.resize(size);
  50. sample_index = 0;
  51. }
  52. }
  53. double performance_sampler::mean_frame_duration() const
  54. {
  55. return std::accumulate(samples.begin(), samples.end(), 0.0) / static_cast<double>(samples.size());
  56. }
  57. } // namespace debug