Created
March 20, 2015 05:26
-
-
Save dermoth/a40537d27f1230aaf663 to your computer and use it in GitHub Desktop.
arista-eosext/PCAPDecoder decode benchmark
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 | |
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) |
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
$ ./bench.py | |
Using binascii, 10,000,000 passes: 20.7187 seconds | |
Using struct, 10,000,000 passes: 2.18561 seconds |
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
>>> 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