forked from dw/scratch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbisect-test.py
48 lines (35 loc) · 1.03 KB
/
bisect-test.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
# http://pythonsweetness.tumblr.com/post/74073984682/cowboy-optimization-bisecting-a-function
import math
import time
import zlib
import sortedfile
target = 3000
lines = sorted(file('/usr/share/dict/words'))
print 'input len:', len(lines)
print 'input avgsize:', sum(map(len, lines)) / float(len(lines))
print 'input steps:', math.log(len(lines), 2)
print 'target size:', target
print
steps = [0]
def compo(i):
dat = zlib.compress(''.join(lines[:i]))
actual = len(dat)
ratio = actual/float(target)
out = (ratio, dat)
print[i, actual, ratio]
steps[0]+=1
return out
t0 = time.time()
doink = sortedfile.bisect_func_right((1.0,), 1, len(lines), compo)
maxlines, (ratio, output) = doink
if maxlines and (ratio > 1.0):
maxlines -= 1
ratio, output = compo(maxlines)
print
print 'time taken:', 1000 * (time.time() - t0)
print 'output steps:', steps
print 'maxlines:', maxlines
print 'maxlines len:', sum(map(len, lines[:maxlines]))
print 'target ratio:', ratio
print 'outlen:', len(output)
#print compo(50, (15000,))