Skip to content

Instantly share code, notes, and snippets.

@itdaniher
Created January 30, 2017 21:08
Show Gist options
  • Save itdaniher/544215e2ebd25495a95d9165633972c6 to your computer and use it in GitHub Desktop.
Save itdaniher/544215e2ebd25495a95d9165633972c6 to your computer and use it in GitHub Desktop.
python-blosc profiling
"""
Small benchmark that compares a plain NumPy array copy against
compression through different compressors in Blosc.
"""
from __future__ import print_function
import numpy as np
import time
import blosc
import ctypes
import sys
arr = np.load(sys.argv[1])
N = arr.shape[0]
clevel = 5
Nexp = np.log10(N)
blosc.print_versions()
arrays = ((arr, "user input"),)
in_ = arrays[0][0]
out_ = np.empty(in_.size, dtype=in_.dtype)
t0 = time.time()
out_ = np.copy(in_)
out_ = ctypes.memmove(out_.__array_interface__['data'][0],
in_.__array_interface__['data'][0], N*8)
tcpy = time.time() - t0
print(" *** ctypes.memmove() *** Time for memcpy():\t%.3f s\t(%.2f MB/s)" % (
tcpy, (N*8 / tcpy) / 2**20))
print("\nTimes for compressing/decompressing with clevel=%d and %d threads" % (
clevel, blosc.ncores))
for (in_, label) in arrays:
print("\n*** %s ***" % label)
for cname in blosc.compressor_list():
for filt in [blosc.NOSHUFFLE, blosc.SHUFFLE, blosc.BITSHUFFLE]:
t0 = time.time()
c = blosc.compress_ptr(in_.__array_interface__['data'][0],
in_.size, in_.dtype.itemsize,
clevel=clevel, shuffle=filt, cname=cname)
tc = time.time() - t0
out = np.empty(in_.size, dtype=in_.dtype)
t0 = time.time()
blosc.decompress_ptr(c, out.__array_interface__['data'][0])
td = time.time() - t0
assert((in_ == out).all())
print(" *** %-8s, %-10s *** %6.3f s (%.2f MB/s) / %5.3f s (%.2f MB/s)" % (
cname, blosc.filters[filt], tc, ((N*8 / tc) / 2**20), td, ((N*8 / td) / 2**20)), end='')
print("\tCompr. ratio: %5.1fx" % (N*8. / len(c)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment