scope_timer.h 637 Bytes
Newer Older
limm's avatar
limm committed
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
// Copyright (c) OpenMMLab. All rights reserved.

#pragma once

#include <sys/time.h>

#include <cstdio>
#include <memory>
#include <string>

class ScopeTimer {
 public:
  ScopeTimer(std::string _name, bool _print = false) : name(_name), print(_print) { begin = now(); }

  ~ScopeTimer() {
    if (!print) {
      return;
    }
    fprintf(stdout, "%s: %ldms\n", name.c_str(), (now() - begin));
  }

  long now() const {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000 + (tv.tv_usec / 1000);
  }

  long cost() const { return now() - begin; }

 private:
  std::string name;
  bool print;
  long begin;
};