-
Notifications
You must be signed in to change notification settings - Fork 0
/
compression_test.py
62 lines (46 loc) · 1.78 KB
/
compression_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from json import load
import lzma
import zlib
from time import time
class CompressionTest:
def __init__(self):
self.load_data()
self.run_lzma()
self.run_zlib()
def load_data(self):
f = open("json_data.txt", "r")
self.data = load(f)
self.raw_data = str(self.data).encode('utf-8')
f.close()
def get_data(self):
return self.data
def run_lzma(self):
start = time()
self.lzma_compressed = lzma.compress(self.raw_data)
self.lzma_compress_time = time() - start
start = time()
lzma.decompress(self.lzma_compressed)
self.lzma_decompress_time = time() - start
def run_zlib(self):
start = time()
self.zlib_compressed = zlib.compress(self.raw_data)
self.zlib_compress_time = time() - start
start = time()
zlib.decompress(self.zlib_compressed)
self.zlib_decompress_time = time() - start
def print_stats(self):
print("Data Length (objs):", len(self.data))
print("Data Length (bytes):", len(self.raw_data))
print("LZMA Compressed Length:", len(self.lzma_compressed))
print("ZLib Compressed Length:", len(self.zlib_compressed))
print("LZMA Compress Time:", self.lzma_compress_time)
print("ZLib Compress Time:", self.zlib_compress_time)
print("LZMA Decompress Time:", self.lzma_decompress_time)
print("ZLib Decompress Time:", self.zlib_decompress_time)
print("")
print("LZMA Compression Ratio:", 1.0 - float(len(self.lzma_compressed))/float(len(self.raw_data)))
print("ZLib Compression Ratio:", 1.0 - float(len(self.zlib_compressed))/float(len(self.raw_data)))
def main():
ct = CompressionTest()
ct.print_stats()
if __name__ == "__main__": main()