Skip to content

Instantly share code, notes, and snippets.

@tiras-j
Created March 29, 2017 15:24
Show Gist options
  • Save tiras-j/5c0357b5a68ad9504d2045febbebd512 to your computer and use it in GitHub Desktop.
Save tiras-j/5c0357b5a68ad9504d2045febbebd512 to your computer and use it in GitHub Desktop.
from __future__ import print_function
import marshal
import struct
import time
import six
header = b'clog'
def struct_serialize(stream, line):
if isinstance(stream, six.text_type):
stream = stream.encode('UTF-8')
if isinstance(line, six.text_type):
line = line.encode('UTF-8')
sizes = (len(stream), len(line))
return header + struct.pack('ii', *sizes) + stream + line
def struct_parse(data):
if data[:4] == header:
sizes = struct.unpack('ii', data[4:12])
return (data[12:12+sizes[0]], data[12+sizes[0]:])
def marshal_serialize(stream, line):
return header + marshal.dumps((stream, line))
def marshal_parse(data):
if data[:4] == header:
return marshal.loads(data[4:])
if __name__ == "__main__":
iters = 100000
stream = 'stream' * 10
line = 'line' * 1000
start = time.time()
for _ in range(iters):
data = struct_serialize(stream, line)
struct_size = len(data)
res = struct_parse(data)
tot = time.time() - start
print ("Struct - Total: {} Avg: {} (size: {})".format(tot, tot/iters, struct_size))
start = time.time()
for _ in range(iters):
data = marshal_serialize(stream, line)
marshal_size = len(data)
res = marshal_parse(data)
tot = time.time() - start
print ("Marshal - Total: {} Avg: {} (size: {})".format(tot, tot/iters, marshal_size))
@tiras-j
Copy link
Author

tiras-j commented Mar 29, 2017

Timings:
Py27

Struct - Total: 0.396899938583 Avg: 3.96899938583e-06 (size: 4072)
Marshal - Total: 1.29385685921 Avg: 1.29385685921e-05 (size: 4079)

Py34

Struct - Total: 0.4712557792663574 Avg: 4.712557792663574e-06 (size: 4072)
Marshal - Total: 1.4795539379119873 Avg: 1.4795539379119873e-05 (size: 4073)

Py35

Struct - Total: 0.4781956672668457 Avg: 4.781956672668457e-06 (size: 4072)
Marshal - Total: 0.5167973041534424 Avg: 5.167973041534424e-06 (size: 4073)

Py36

Struct - Total: 0.5680632591247559 Avg: 5.6806325912475585e-06 (size: 4072)
Marshal - Total: 0.564603328704834 Avg: 5.64603328704834e-06 (size: 4073)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment