forked from gtimelog/gtimelog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.py
executable file
·222 lines (182 loc) · 4.89 KB
/
benchmark.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/python3
import gc
import os
import sys
import time
from operator import itemgetter
pkgdir = os.path.join(os.path.dirname(__file__), 'src')
sys.path.insert(0, pkgdir)
from gtimelog.settings import Settings
from gtimelog.timelog import TimeLog, parse_datetime
fns = []
def mark(fn):
fns.append(fn)
return fn
def unmark(fn):
return fn
def benchmark(fn, correct_output):
gc.collect()
print("{}:".format(fn.__name__), end="")
m = float("inf")
n = 0
output = fn()
if output != correct_output:
print(" [NB incorrect output]")
else:
print()
t00 = time.time()
while n < 3 or time.time() - t00 < 3:
t0 = time.time()
fn()
t1 = time.time()
d = t1 - t0
m = min(m, d)
print("\r{:.3f}s".format(d), end="")
sys.stdout.flush()
n += 1
if n > 100:
break
tot = time.time() - t00
print("\rmin {:.3f}s avg {:.3f}s (n={})\n".format(m, tot / n, n))
@unmark
def just_read():
filename = Settings().get_timelog_file()
open(filename).readlines()
@unmark
def split():
filename = Settings().get_timelog_file()
for line in open(filename):
if ': ' not in line:
continue
time, entry = line.split(': ', 1)
@unmark
def parse_one():
filename = Settings().get_timelog_file()
for line in open(filename):
if ': ' not in line:
continue
time, entry = line.split(': ', 1)
try:
time = parse_datetime(time)
except ValueError:
continue
@unmark
def parse_two(): # slower than parse_one
filename = Settings().get_timelog_file()
for line in open(filename):
try:
time, entry = line.split(': ', 1)
time = parse_datetime(time)
except ValueError:
continue
@unmark
def parse_three(): # fastest
filename = Settings().get_timelog_file()
for line in open(filename):
time, sep, entry = line.partition(': ')
if not sep:
continue
try:
time = parse_datetime(time)
except ValueError:
continue
@unmark
def parse_and_strip():
filename = Settings().get_timelog_file()
for line in open(filename):
time, sep, entry = line.partition(': ')
if not sep:
continue
try:
time = parse_datetime(time)
except ValueError:
continue
entry = entry.strip()
@unmark
def parse_and_collect():
items = []
filename = Settings().get_timelog_file()
for line in open(filename):
time, sep, entry = line.partition(': ')
if not sep:
continue
try:
time = parse_datetime(time)
except ValueError:
continue
entry = entry.strip()
items.append((time, entry))
return items
@unmark
def parse_and_sort_incorrectly():
items = []
filename = Settings().get_timelog_file()
for line in open(filename):
time, sep, entry = line.partition(': ')
if not sep:
continue
try:
time = parse_datetime(time)
except ValueError:
continue
entry = entry.strip()
items.append((time, entry))
items.sort() # XXX: can reorder lines
return items
@mark
def parse_and_sort():
items = []
filename = Settings().get_timelog_file()
for line in open(filename):
time, sep, entry = line.partition(': ')
if not sep:
continue
try:
time = parse_datetime(time)
except ValueError:
continue
entry = entry.strip()
items.append((time, entry))
items.sort(key=itemgetter(0))
return items
@mark
def parse_and_sort_unicode():
items = []
filename = Settings().get_timelog_file()
for line in open(filename, 'rb').read().decode('UTF-8').splitlines():
time, sep, entry = line.partition(': ')
if not sep:
continue
try:
time = parse_datetime(time)
except ValueError:
continue
entry = entry.strip()
items.append((time, entry))
items.sort(key=itemgetter(0))
return items
@unmark
def parse_and_sort_unicode_piecemeal():
items = []
filename = Settings().get_timelog_file()
for line in open(filename, 'rb'):
time, sep, entry = line.partition(b': ')
if not sep:
continue
try:
time = parse_datetime(time.decode('ASCII'))
except (ValueError, UnicodeError):
continue
entry = entry.strip().decode('UTF-8')
items.append((time, entry))
items.sort(key=itemgetter(0))
return items
@mark
def full():
return TimeLog(Settings().get_timelog_file(), Settings().virtual_midnight).items
def main():
correct = full()
for fn in fns:
benchmark(fn, correct)
if __name__ == '__main__':
main()