-
-
Save girasquid/3427762 to your computer and use it in GitHub Desktop.
compressing a bunch of id's to construct a url
This file contains hidden or 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 random | |
import string | |
import base64 | |
import zlib | |
import bz2 | |
random_ids = [''.join([random.choice(string.ascii_lowercase) for x in range(8)]) for n in range(1000)] | |
long_string = ','.join(random_ids) | |
b64_string = base64.urlsafe_b64encode(long_string) | |
compressed_string = zlib.compress(long_string, 9) | |
b64_compressed_string = base64.urlsafe_b64encode(compressed_string) | |
bz2_compressed_string = bz2.compress(long_string, 9) | |
b64_bz2_compressed_string = base64.urlsafe_b64encode(bz2_compressed_string) | |
len(long_string) | |
len(b64_string) | |
len(compressed_string) | |
len(b64_compressed_string) | |
len(bz2_compressed_string) | |
len(b64_bz2_compressed_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You'll get slightly better compression if you use bz2 instead of zlib - bz2 will give you back some binary data, but because you're base64ing it you should be fine. My results (bz2 is last set):
8999
12000
5646
7528
5497
7332