1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-18 13:55:11 -05:00
polybar/include/debug.hpp

52 lines
1.2 KiB
C++
Raw Normal View History

2016-06-14 23:32:35 -04:00
#pragma once
#ifndef DEBUG
#error "Not a debug build..."
#endif
2016-11-20 17:04:31 -05:00
#include <chrono>
2017-01-10 21:07:28 -05:00
#include <cstdio>
2016-06-14 23:32:35 -04:00
#include "common.hpp"
POLYBAR_NS
2016-06-14 23:32:35 -04:00
namespace debug_util {
/**
* Wrapper that starts tracking the time when created
* and reports the duration when it goes out of scope
*/
class scope_timer {
public:
using clock_t = std::chrono::high_resolution_clock;
using duration_t = std::chrono::milliseconds;
explicit scope_timer() : m_start(clock_t::now()) {}
~scope_timer() {
2017-01-10 21:07:28 -05:00
printf("%lums\n", std::chrono::duration_cast<duration_t>(clock_t::now() - m_start).count());
}
private:
clock_t::time_point m_start;
};
inline unique_ptr<scope_timer> make_scope_timer() {
return make_unique<scope_timer>();
}
template <class T>
void execution_speed(const T& expr) noexcept {
auto start = std::chrono::high_resolution_clock::now();
expr();
auto finish = std::chrono::high_resolution_clock::now();
2017-01-10 21:07:28 -05:00
printf("execution speed: %lums\n", std::chrono::duration_cast<std::chrono::milliseconds>(finish - start).count());
}
template <class T>
void memory_usage(const T& object) noexcept {
2017-01-10 21:07:28 -05:00
printf("memory usage: %lub\n", sizeof(object));
}
2016-06-14 23:32:35 -04:00
}
POLYBAR_NS_END