Created
August 22, 2018 20:02
-
-
Save jamadden/acab7901f9f43faf7c0f47c11b3d8f8e to your computer and use it in GitHub Desktop.
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
""" | |
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
import perf | |
import struct | |
from struct import pack | |
from struct import unpack | |
INNER_LOOPS = 1000 | |
def p64(v): | |
return pack('>Q', v) | |
def u64(v): | |
return unpack('>Q', v)[0] | |
OID_STRUCT = struct.Struct(">Q") | |
struct_pack = OID_STRUCT.pack | |
struct_unpack = OID_STRUCT.unpack | |
def struct_unpack_real(i): | |
return struct_unpack(i)[0] | |
def struct_unpack_real_kwarg(i, _unpack=struct_unpack): | |
return _unpack(i)[0] | |
def struct_pack_tbinfo(i): | |
__traceback_info__ = i | |
return struct_pack(i) | |
def struct_pack_catch(i): | |
try: | |
return struct_pack(i) | |
except struct.error: | |
raise TypeError() | |
def struct_pack_catch_kwarg(i, _pack=struct_pack): | |
try: | |
return _pack(i) | |
except struct.error: | |
raise TypeError() | |
def driver(loops, p, up): | |
begin = perf.perf_counter() | |
for _ in range(loops): | |
for _ in range(INNER_LOOPS): | |
up(p(123456789)) | |
end = perf.perf_counter() | |
return end - begin | |
def main(): | |
runner = perf.Runner() | |
for name, p, up in ( | |
('current', p64, u64), | |
('struct method direct: u64(p64())', struct_pack, struct_unpack), | |
('struct pack direct unpack indirect: u64(p64())', struct_pack, struct_unpack_real), | |
('struct pack tbinfo unpack indirect: u64(p64())', struct_pack_tbinfo, struct_unpack_real), | |
('struct pack catch unpack indirect: u64(p64())', struct_pack_catch, struct_unpack_real), | |
('struct pack catch unpack indirect kwarg: u64(p64())', struct_pack_catch_kwarg, struct_unpack_real_kwarg), | |
): | |
runner.bench_time_func(name, | |
driver, | |
p, up, | |
inner_loops=INNER_LOOPS) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment