profiler.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. Minetest
  3. Copyright (C) 2015 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "profiler.h"
  17. static Profiler main_profiler;
  18. Profiler *g_profiler = &main_profiler;
  19. ScopeProfiler::ScopeProfiler(
  20. Profiler *profiler, const std::string &name, ScopeProfilerType type) :
  21. m_profiler(profiler),
  22. m_name(name), m_type(type)
  23. {
  24. if (m_profiler)
  25. m_timer = new TimeTaker(m_name);
  26. }
  27. ScopeProfiler::~ScopeProfiler()
  28. {
  29. if (!m_timer)
  30. return;
  31. float duration_ms = m_timer->stop(true);
  32. float duration = duration_ms / 1000.0;
  33. if (m_profiler) {
  34. switch (m_type) {
  35. case SPT_ADD:
  36. m_profiler->add(m_name, duration);
  37. break;
  38. case SPT_AVG:
  39. m_profiler->avg(m_name, duration);
  40. break;
  41. case SPT_GRAPH_ADD:
  42. m_profiler->graphAdd(m_name, duration);
  43. break;
  44. }
  45. }
  46. delete m_timer;
  47. }