Skip to content

Instantly share code, notes, and snippets.

@kajuberdut
Created May 30, 2021 21:51
Show Gist options
  • Save kajuberdut/b59dfafa71cc58a1aa879f8e17401512 to your computer and use it in GitHub Desktop.
Save kajuberdut/b59dfafa71cc58a1aa879f8e17401512 to your computer and use it in GitHub Desktop.
A quick comparison of string storage methods for lists of ints Join vs Pickle
import base64
import pickle
from random import randrange
from sys import getsizeof
from time import perf_counter_ns
from icecream import ic
lol = [[randrange(10) for i in range(randrange(10))] for i in range(10000)]
start = perf_counter_ns()
encoded = [",".join([str(i) for i in l]) for l in lol]
print(f"Encoded as string: {(perf_counter_ns() - start)/1e+9} Seconds\n\n")
ic(encoded[10])
ic(getsizeof(encoded[10]))
start = perf_counter_ns()
decoded = [[int(i) for i in s.split(",") if i] for s in encoded]
print(f"Decoded from string: {(perf_counter_ns() - start)/1e+9} Seconds\n\n")
ic(decoded[10])
start = perf_counter_ns()
encoded2 = [pickle.dumps(l) for l in lol]
print(f"Pickled: {(perf_counter_ns() - start)/1e+9} Seconds\n\n")
ic(encoded2[10])
ic(getsizeof(encoded2[10]))
start = perf_counter_ns()
decoded2 = [pickle.loads(p) for p in encoded2]
print(f"Unpickled: {(perf_counter_ns() - start)/1e+9} Seconds\n\n")
ic(decoded2[10])
start = perf_counter_ns()
encoded3 = [base64.b64encode(pickle.dumps(l)).decode("utf-8") for l in lol]
print(f"Pickled+Base64: {(perf_counter_ns() - start)/1e+9} Seconds\n\n")
ic(encoded3[10])
ic(getsizeof(encoded3[10]))
start = perf_counter_ns()
decoded3 = [pickle.loads(base64.decodebytes(p.encode())) for p in encoded3]
print(f"Un-Base64+Unpickled: {(perf_counter_ns() - start)/1e+9} Seconds\n\n")
ic(decoded3[10])
Encoded as string: 0.0196791 Seconds
ic| encoded[10]: '6,9,0,7,9,7'
ic| getsizeof(encoded[10]): 60
Decoded from string: 0.026051 Seconds
ic| decoded[10]: [6, 9, 0, 7, 9, 7]
Pickled: 0.0054405 Seconds
ic| encoded2[10]: (b'\x80\x04\x95\x11\x00\x00\x00\x00\x00\x00\x00]\x94(K\x06K\tK\x00K\x07K\tK\x07e.')
ic| getsizeof(encoded2[10]): 61
Unpickled: 0.005127 Seconds
ic| decoded2[10]: [6, 9, 0, 7, 9, 7]
Pickled+Base64: 0.0197671 Seconds
ic| encoded3[10]: 'gASVEQAAAAAAAABdlChLBksJSwBLB0sJSwdlLg=='
ic| getsizeof(encoded3[10]): 89
Un-Base64+Unpickled: 0.0308701 Seconds
ic| decoded3[10]: [6, 9, 0, 7, 9, 7]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment