Skip to content

Instantly share code, notes, and snippets.

@laiyonghao
Created April 28, 2012 14:11
Show Gist options
  • Save laiyonghao/2519354 to your computer and use it in GitHub Desktop.
Save laiyonghao/2519354 to your computer and use it in GitHub Desktop.
struct-benchmark
#!/usr/bin/env python
from timeit import timeit
setup = '''
from ctypes import create_string_buffer
from struct import Struct
buff = create_string_buffer(1024)
payload = 'hello' * 30
a, b, c = 1, 3.0, 'world'
packer = Struct('!Id5s150s')
def use_pack():
for i in xrange(1000000):
packer.pack(a, b, c, payload)
def use_pack_into():
for i in xrange(1000000):
packer.pack_into(buff, 0, a, b, c, payload)
'''
stmt1 = 'use_pack()'
stmt2 = 'use_pack_into()'
print timeit(stmt1, setup, number = 10)
print timeit(stmt2, setup, number = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment