File: debug.hpp

package info (click to toggle)
polybar 3.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,108 kB
  • sloc: cpp: 30,424; python: 3,750; sh: 284; makefile: 83
file content (51 lines) | stat: -rw-r--r-- 1,228 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once

#ifndef DEBUG
#error "Not a debug build..."
#endif

#include <chrono>
#include <cstdio>

#include "common.hpp"

POLYBAR_NS

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() {
      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();
    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 {
    printf("memory usage: %lub\n", sizeof(object));
  }
}

POLYBAR_NS_END