forked from taku910/crfpp
-
Notifications
You must be signed in to change notification settings - Fork 11
/
timer.h
58 lines (49 loc) · 1.36 KB
/
timer.h
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
52
53
54
55
56
57
58
//
// CRF++ -- Yet Another CRF toolkit
//
// $Id: timer.h 1588 2007-02-12 09:03:39Z taku $;
//
// Copyright(C) 2005-2007 Taku Kudo <[email protected]>
//
#ifndef CRFPP_TIMER_H_
#define CRFPP_TIMER_H_
#include <ctime>
#include <iostream>
#include <string>
#include <limits>
namespace CRFPP {
class timer {
public:
explicit timer() { start_time_ = std::clock(); }
void restart() { start_time_ = std::clock(); }
double elapsed() const {
return static_cast<double>(std::clock() - start_time_) / CLOCKS_PER_SEC;
}
double elapsed_max() const {
// return (static_cast<double>(std::numeric_limits<std::clock_t>::max())
// - static_cast<double>(start_time_)) /
// static_cast<double>(CLOCKS_PER_SEC);
return 0.0;
}
double elapsed_min() const {
return static_cast<double>(1.0 / CLOCKS_PER_SEC);
}
private:
std::clock_t start_time_;
};
class progress_timer : public timer {
public:
explicit progress_timer(std::ostream & os = std::cout) : os_(os) {}
virtual ~progress_timer() {
std::istream::fmtflags old_flags = os_.setf(std::istream::fixed,
std::istream::floatfield);
std::streamsize old_prec = os_.precision(2);
os_ << elapsed() << " s\n" << std::endl;
os_.flags(old_flags);
os_.precision(old_prec);
}
private:
std::ostream & os_;
};
}
#endif