Last active
June 27, 2017 16:59
-
-
Save lebedov/dfcbbb01cdd4cd019471e87575f12af1 to your computer and use it in GitHub Desktop.
Compare timing of record parsing in construct and numpy.
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
#!/usr/bin/env python | |
""" | |
Compare timing of record parsing in construct and numpy. | |
""" | |
import time | |
from construct import LazyStruct, Struct, Int16un, Int8un | |
import numpy as np | |
def func_time(f, *args): | |
start = time.time() | |
f(*args) | |
return time.time()-start | |
m = 200 | |
MyStruct = Struct('x' / Int16un, | |
'y' / Int16un, | |
'data' / Int8un[m]) | |
MyLazyStruct = LazyStruct('x' / Int16un, | |
'y' / Int16un, | |
'data' / Int8un[m]) | |
rectype = np.dtype([('x', np.uint16), ('y', np.uint16), ('data', np.uint8, m)]) | |
n = 100 | |
t_construct = 0.0 | |
t_numpy = 0.0 | |
for i in range(n): | |
s = MyStruct.build(dict(x=1, y=1, data=range(m))) | |
t_construct = func_time(MyStruct.parse, s) | |
t_construct_lazy = func_time(MyLazyStruct.parse, s) | |
t_numpy = func_time(np.frombuffer, s, rectype) | |
print 'construct: ', t_construct/n | |
print 'construct (lazy): ', t_construct_lazy/n | |
print 'numpy: ', t_numpy/n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment