Last active
June 2, 2021 17:04
-
-
Save Rahul91/aceabac54bf510c0126cae3d4f0742be to your computer and use it in GitHub Desktop.
benchmark_compressions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json, os, sys, zlib, bz2, lzma | |
from datetime import datetime | |
def timeit(func): | |
def wrapper(*args, **kwargs): | |
st = datetime.now() | |
compressed_data = func(*args, **kwargs) | |
print('Time take: {} milliseconds'.format((datetime.now() - st).microseconds / 1000)) | |
return compressed_data | |
return wrapper | |
@timeit | |
def compress(sample_data, algo): | |
print('Algo: {}'.format(algo)) | |
print('='*30) | |
uncompressed_data_size = sys.getsizeof(sample_data) | |
print('sample_data size: {} kb'.format(uncompressed_data_size / 1024)) | |
if algo == 'zlib': | |
compressed_data = zlib.compress(sample_data) | |
elif algo == 'bz2': | |
compressed_data = bz2.compress(sample_data) | |
elif algo == 'lzma': | |
compressed_data = lzma.compress(sample_data) | |
compressed_data_size = sys.getsizeof(compressed_data) | |
print('Size of compressed output: {} kb'.format(compressed_data_size / 1024)) | |
print('Compression Ratio: {}'.format(uncompressed_data_size / compressed_data_size)) | |
return compressed_data | |
with open('sample.json') as file: | |
sample_data = json.load(file) | |
_ = compress(str.encode(json.dumps(sample_data)), 'zlib') | |
Algo: zlib | |
============================== | |
sample_data size: 3515.5859375 kb | |
Size of compressed output: 193.5419921875 kb | |
Compression Ratio: 18.164460837491863 | |
Time take: 36.7 milliseconds | |
_ = compress(str.encode(json.dumps(sample_data)), 'bz2') | |
Algo: bz2 | |
============================== | |
sample_data size: 3515.5859375 kb | |
Size of compressed output: 126.2041015625 kb | |
Compression Ratio: 27.856352479629816 | |
Time take: 403.843 milliseconds | |
_ = compress(str.encode(json.dumps(sample_data)), 'lzma') | |
Algo: lzma | |
============================== | |
sample_data size: 3515.5859375 kb | |
Size of compressed output: 117.3173828125 kb | |
Compression Ratio: 29.96645384698626 | |
Time take: 333.351 milliseconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment