-
Notifications
You must be signed in to change notification settings - Fork 5
/
evaluate.py
138 lines (121 loc) · 4.71 KB
/
evaluate.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
"""
检测压缩算法的准确性, 评估压缩率, 效率
"""
import time
from bitio import BitReader
from RunLength import RunLength
from Huffman import Huffman
from LZW import LZW
class FileBits:
def __init__(self, fp):
self.f = open(fp, 'rb')
self.bit_stream = BitReader(self.f)
self.bits = []
while True:
x = self.bit_stream.read_bit()
if not self.bit_stream.read:
break
self.bits.append(x)
def __eq__(self, other):
if len(self.bits) != len(other.bits):
return False
return self.bits == other.bits
def __repr__(self):
b = ['1' if x else '0' for x in self.bits]
return ''.join(b)
def __len__(self):
return len(self.bits)
def __del__(self):
self.f.close()
def evaluate_compress_expand(ori_files, com_files, exp_files, algs):
for of, cf, ef in zip(ori_files, com_files, exp_files):
b_com = time.time()
algs.compress(of, cf) # 压缩, origin -> compress
e_com = time.time()
algs.expand(cf, ef) # 解压缩, compress -> origin
e_exp = time.time()
of_bits, cf_bits, ef_bits = FileBits(of), FileBits(cf), FileBits(ef)
print(of)
print('bits', len(of_bits), '->', len(cf_bits), ',rate {:.3f}'.format(len(cf_bits) / len(of_bits)))
print('compress {:.3f}s, expand {:.3f}s'.format(e_com - b_com, e_exp - e_com))
assert of_bits == ef_bits # origin bits == expand bits
print()
def evaluate_run_length():
print('-' * 30, 'RunLength', '-' * 30)
ori_files = ['data/4runs.bin',
'data/abra.txt',
'data/q32x48.bin',
'data/q64x96.bin']
com_files = ['temp_files/4runs.bin.rl',
'temp_files/abra.txt.rl',
'temp_files/q32x48.bin.rl',
'temp_files/q64x96.bin.rl']
exp_files = ['temp_files/4runs.bin',
'temp_files/abra.txt',
'temp_files/q32x48.bin',
'temp_files/q64x96.bin']
evaluate_compress_expand(ori_files, com_files, exp_files, RunLength)
def evaluate_huffman():
print('-' * 30, 'Huffman', '-' * 30)
ori_files = ['data/4runs.bin',
'data/abra.txt',
'data/q32x48.bin',
'data/q64x96.bin',
'data/tinytinyTale.txt',
'data/tinyTale.txt',
'data/medTale.txt',
'data/tale.txt']
com_files = ['temp_files/4runs.bin.huffman',
'temp_files/abra.txt.huffman',
'temp_files/q32x48.bin.huffman',
'temp_files/q64x96.bin.huffman',
'temp_files/tinytinyTale.txt.huffman',
'temp_files/tinyTale.txt.huffman',
'temp_files/medTale.txt.huffman',
'temp_files/tale.txt.huffman']
exp_files = ['temp_files/4runs.bin',
'temp_files/abra.txt',
'temp_files/q32x48.bin',
'temp_files/q64x96.bin',
'temp_files/tinytinyTale.txt',
'temp_files/tinyTale.txt',
'temp_files/medTale.txt',
'temp_files/tale.txt']
evaluate_compress_expand(ori_files, com_files, exp_files, Huffman)
def evaluate_lzw():
print('-' * 30, 'LZW', '-' * 30)
ori_files = ['data/4runs.bin',
'data/abra.txt',
'data/q32x48.bin',
'data/q64x96.bin',
'data/tinytinyTale.txt',
'data/tinyTale.txt',
'data/medTale.txt',
'data/tale.txt',
'data/ababLZW.txt',
'data/abraLZW.txt']
com_files = ['temp_files/4runs.bin.lzw',
'temp_files/abra.txt.lzw',
'temp_files/q32x48.bin.lzw',
'temp_files/q64x96.bin.lzw',
'temp_files/tinytinyTale.txt.lzw',
'temp_files/tinyTale.txt.lzw',
'temp_files/medTale.txt.lzw',
'temp_files/tale.txt.lzw',
'temp_files/ababLZW.txt.lzw',
'temp_files/abraLZW.txt.lzw']
exp_files = ['temp_files/4runs.bin',
'temp_files/abra.txt',
'temp_files/q32x48.bin',
'temp_files/q64x96.bin',
'temp_files/tinytinyTale.txt',
'temp_files/tinyTale.txt',
'temp_files/medTale.txt',
'temp_files/tale.txt',
'temp_files/ababLZW.txt',
'temp_files/abraLZW.txt']
evaluate_compress_expand(ori_files, com_files, exp_files, LZW)
if __name__ == '__main__':
evaluate_run_length()
evaluate_huffman()
evaluate_lzw()