-
Notifications
You must be signed in to change notification settings - Fork 12
/
Timer.cpp
87 lines (87 loc) · 2.6 KB
/
Timer.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "Timer.hpp"
#include "Runner.hpp"
#include <fstream>
#include <iostream>
void Timer::addTask(int64_t time) {
mTotTime += time;
mMaxTime = std::max(mMaxTime, time);
}
bool Timer::isTLE(int64_t time) const {
return (mMode == TimeMode::perTask ? time : time +
mTotTime) > mTimeLimit;
}
Timer::Timer(const Option& opt)
: mTotTime(0), mMaxTime(0),
mTimeLimit(
opt.get<int64_t>("TimeLimit", 1000000)),
mLocalSamples(0), mRemoteSamples(0),
mMode(opt.get("TimeMode", TimeMode::perTask)),
mSamples(opt.get<fs::path>("TimeSamples", "")) {
if(fs::exists(mSamples)) {
std::ifstream sam(mSamples);
while(sam) {
int64_t a = 0, b = 0;
sam >> a >> b;
mLocalSamples += a, mRemoteSamples += b;
}
if(mRemoteSamples) {
mTimeLimit = mTimeLimit * mLocalSamples /
mRemoteSamples;
std::cout
<< "time scaling factor="
<< static_cast<FT>(mLocalSamples) /
mRemoteSamples
<< std::endl;
}
} else {
std::ofstream out(mSamples);
}
}
bool Timer::isTLE() const {
return mMode == TimeMode::allTask &&
mTotTime > mTimeLimit;
}
void Timer::report() const {
if(isTLE())
std::cout << toString(Status::TLE)
<< std::endl;
else {
std::cout << "TotTime " << mTotTime / 1000.0
<< " ms";
if(mTimeLimit && mMode == TimeMode::allTask)
std::cout << "("
<< mTotTime * 100 / mTimeLimit
<< "%)";
std::cout << std::endl;
std::cout << "MaxTime " << mMaxTime / 1000.0
<< " ms";
if(mTimeLimit && mMode == TimeMode::perTask)
std::cout << "("
<< mMaxTime * 100 / mTimeLimit
<< "%)";
std::cout << std::endl;
}
}
int64_t Timer::remain() const {
return mMode == TimeMode::perTask ?
mTimeLimit :
mTimeLimit - mTotTime;
}
void Timer::addSample() {
std::ofstream sam(mSamples, std::ios::app);
if(!sam.is_open())
return;
line("Add Time Sample");
int64_t remote = 0,
local = (mTotTime - 1) / 1000 + 1;
if(mLocalSamples)
std::cout << "OJ's time(prediction = "
<< local * mRemoteSamples /
mLocalSamples
<< " ms): " << std::flush;
else
std::cout << "OJ's time: " << std::flush;
std::cin >> remote;
if(remote)
sam << local << ' ' << remote << std::endl;
}