Skip to content

Instantly share code, notes, and snippets.

@dermoth
Created March 20, 2015 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dermoth/a40537d27f1230aaf663 to your computer and use it in GitHub Desktop.
Save dermoth/a40537d27f1230aaf663 to your computer and use it in GitHub Desktop.
arista-eosext/PCAPDecoder decode benchmark
#!/usr/bin/env python
import timeit
import locale
locale.setlocale(locale.LC_ALL, '') # Default user locale
#locale.setlocale(locale.LC_ALL, 'en_US') # ... or force it with this
runcnt=10000000
result = timeit.timeit(
"""(asicTime, utc, deviceId) = (
int( '0x' + binascii.hexlify( v[ 0 : 8 ] ), 0 ),
int( '0x' + binascii.hexlify( v[ 8 : 16 ] ), 0 ),
int( '0x' + binascii.hexlify( v[ 40 : 42 ] ), 0 ),
)""",
setup="""import binascii; v='002863c2d9f3784e13cd081ccba1a7540000000000000000002863c2d9f60a8e000000000093fa0a006400340100'.decode('hex')""",
number=runcnt
)
print locale.format_string("Using binascii, %d passes: %g seconds", (runcnt, result), grouping=True)
result = timeit.timeit(
"""(asicTime, utc, deviceId) = struct.unpack('>QQxxxxxxxxxxxxxxxxxxxxxxxxHxxxx', v)""",
setup="""import struct; v='002863c2d9f3784e13cd081ccba1a7540000000000000000002863c2d9f60a8e000000000093fa0a006400340100'.decode('hex')""",
number=runcnt
)
print locale.format_string("Using struct, %d passes: %g seconds", (runcnt, result), grouping=True)
$ ./bench.py
Using binascii, 10,000,000 passes: 20.7187 seconds
Using struct, 10,000,000 passes: 2.18561 seconds
>>> import binascii
>>> import struct
>>> v='002863c2d9f3784e13cd081ccba1a7540000000000000000002863c2d9f60a8e000000000093fa0a006400340100'.decode('hex')
>>>
>>> (asicTime, utc, deviceId) = (
... int( '0x' + binascii.hexlify( v[ 0 : 8 ] ), 0 ),
... int( '0x' + binascii.hexlify( v[ 8 : 16 ] ), 0 ),
... int( '0x' + binascii.hexlify( v[ 40 : 42 ] ), 0 ),
... )
>>> (asicTime, utc, deviceId)
(11368687599843406, 1426805576714790740, 100)
>>> struct.unpack('>QQxxxxxxxxxxxxxxxxxxxxxxxxHxxxx', v)
(11368687599843406, 1426805576714790740, 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment