File: decodetree.py

package info (click to toggle)
python-bitarray 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,288 kB
  • sloc: python: 11,456; ansic: 7,657; makefile: 73; sh: 6
file content (36 lines) | stat: -rw-r--r-- 874 bytes parent folder | download | duplicates (2)
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
from random import random, randrange
from time import perf_counter

from bitarray import bitarray, decodetree
from bitarray.util import huffman_code


N = 100_000

# create Huffman code for N symbols
code = huffman_code({i: random() for i in range(N)})
print(len(code))

# create the decodetree object
t0 = perf_counter()
tree = decodetree(code)
print('decodetree(code):  %9.6f ms' % (1000.0 * (perf_counter() - t0)))

print(tree.nodes())
plain = [randrange(N) for _ in range(100)]

a = bitarray()
a.encode(code, plain)

# decode using the code dictionary
t0 = perf_counter()
res = a.decode(code)
print('decode(code):  %9.6f ms' % (1000.0 * (perf_counter() - t0)))
assert res == plain

# decode using the decodetree
t0 = perf_counter()
res = a.decode(tree)
print('decode(tree):  %9.6f ms' % (1000.0 * (perf_counter() - t0)))
assert res == plain
assert tree.todict() == code