Skip to content

Instantly share code, notes, and snippets.

@PM2Ring
Created June 29, 2017 16:26
Show Gist options
  • Save PM2Ring/c79ec528e9e9e30df0113ecf0bf03342 to your computer and use it in GitHub Desktop.
Save PM2Ring/c79ec528e9e9e30df0113ecf0bf03342 to your computer and use it in GitHub Desktop.
Bit packing speed tests: Numpy vs pure Python 3.
#!/usr/bin/env python3
''' Test speeds of several bit packing algorithms
Written by PM 2Ring 2017.06.29
'''
import numpy as np
from timeit import Timer
def packbits_all(b):
return int(''.join(map(str, b)), 2).to_bytes(-(-len(b) // 8), 'big')
def packbits_chunks(b):
return bytes(int(u, 2) for u in map(''.join, zip(*[map(str, b)] * 8)))
def packbits_numpy(b):
return np.packbits(b)
funcs = (
packbits_all,
packbits_chunks,
packbits_numpy,
)
a = list(range(256))
print(a)
data = [int(v) for u in a for v in f'{u:08b}']
loops = 1 << 9
for i in range(8):
size = len(data)
print('\nSize: {}, Loops: {}'.format(size, loops))
timings = []
for func in funcs:
name = func.__name__
cmd = name + '(data)'
setup = 'from __main__ import np, data, ' + name
t = Timer(cmd, setup)
result = sorted(t.repeat(3, loops))
timings.append((result, name))
timings.sort()
for result, name in timings:
print('{:15} : {}'.format(name, result))
loops >>= 1
data += data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment