-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
85 lines (60 loc) · 2.32 KB
/
plot.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
# Tämä tiedosto ei varsinaisesti kuulu projektiin,
# mutta laitan sen nyt tänne, jotta
# nähdään miten tarkalleen nopeutta testattiin
import pakkaus
import matplotlib.pyplot as plt
import time
with open("enwik8", "rb") as f:
data = f.read()
lzw_comp_results = []
huf_comp_results = []
lzw_uncomp_results = []
huf_uncomp_results = []
lzw_efficiency = []
huf_efficiency = []
size = 4096
while size < len(data):
start_time = time.time()
lzw_packed = pakkaus.lzw.compress(data[0:size])
lzw_comp_results.append((size, time.time() - start_time))
lzw_efficiency.append((size, len(lzw_packed) / size))
start_time = time.time()
lzw_unpacked = pakkaus.lzw.uncompress(lzw_packed)
lzw_uncomp_results.append((size, time.time() - start_time))
start_time = time.time()
huf_packed = pakkaus.huffman.pack_data(data[0:size])
huf_comp_results.append((size, time.time() - start_time))
huf_efficiency.append((size, len(huf_packed) / size))
start_time = time.time()
huf_unpacked = pakkaus.huffman.unpack_data(huf_packed)
huf_uncomp_results.append((size, time.time() - start_time))
size *= 2
# https://stackoverflow.com/questions/1094841/get-human-readable-version-of-file-size
def sizeof_fmt(num, _):
for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}B"
num /= 1024.0
return f"{num:.1f}YiB"
fig, (ax1, ax2) = plt.subplots(2)
ax1.xaxis.set_major_formatter(sizeof_fmt)
fig.suptitle("enwik8 pakkaus- ja purkunopeus")
ax1.plot(*zip(*lzw_comp_results), label="LZW Pakkausnopeus")
ax1.plot(*zip(*huf_comp_results), label="Huffman Pakkausnopeus")
ax1.legend(loc="best")
fig.supxlabel("Syötteen koko tavuina")
fig.supylabel("Aika sekunteina")
ax2.xaxis.set_major_formatter(sizeof_fmt)
ax2.plot(*zip(*lzw_uncomp_results), label="LZW Purkamisnopeus")
ax2.plot(*zip(*huf_uncomp_results), label="Huffman Purkamisnopeus")
ax2.legend(loc="best")
fig, ax = plt.subplots()
fig.suptitle("enwik8 pakkaussuhde")
ax.xaxis.set_major_formatter(sizeof_fmt)
ax.yaxis.set_major_formatter(lambda x, _: f"{x*100:.1f}%")
ax.plot(*zip(*lzw_efficiency), label="LZW Pakkaussuhde")
ax.plot(*zip(*huf_efficiency), label="Huffman Pakkaussuhde")
ax.legend(loc="best")
plt.xlabel("Syötteen koko tavuina")
plt.ylabel("Pakkaussuhde")
plt.show()