|
| 1 | +""" |
| 2 | +Test the function call overhead of ctypes. |
| 3 | +""" |
| 4 | +import pyperf |
| 5 | + |
| 6 | + |
| 7 | +import ctypes |
| 8 | +import importlib.util |
| 9 | + |
| 10 | + |
| 11 | +spec = importlib.util.find_spec("bm_ctypes.cmodule") |
| 12 | +if spec is None: |
| 13 | + raise ImportError("Can't find bm_ctypes.cmodule shared object file") |
| 14 | +ext = ctypes.cdll.LoadLibrary(spec.origin) |
| 15 | + |
| 16 | + |
| 17 | +def benchmark_argtypes(loops): |
| 18 | + void_foo_void = ext.void_foo_void |
| 19 | + void_foo_void.argtypes = [] |
| 20 | + void_foo_void.restype = None |
| 21 | + |
| 22 | + int_foo_int = ext.void_foo_int |
| 23 | + int_foo_int.argtypes = [ctypes.c_int] |
| 24 | + int_foo_int.restype = ctypes.c_int |
| 25 | + |
| 26 | + void_foo_int = ext.void_foo_int |
| 27 | + void_foo_int.argtypes = [ctypes.c_int] |
| 28 | + void_foo_int.restype = None |
| 29 | + |
| 30 | + void_foo_int_int = ext.void_foo_int_int |
| 31 | + void_foo_int_int.argtypes = [ctypes.c_int, ctypes.c_int] |
| 32 | + void_foo_int_int.restype = None |
| 33 | + |
| 34 | + void_foo_int_int_int = ext.void_foo_int_int_int |
| 35 | + void_foo_int_int_int.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int] |
| 36 | + void_foo_int_int_int.restype = None |
| 37 | + |
| 38 | + void_foo_int_int_int_int = ext.void_foo_int_int_int_int |
| 39 | + void_foo_int_int_int_int.argtypes = [ |
| 40 | + ctypes.c_int, |
| 41 | + ctypes.c_int, |
| 42 | + ctypes.c_int, |
| 43 | + ctypes.c_int, |
| 44 | + ] |
| 45 | + void_foo_int_int_int_int.restype = None |
| 46 | + |
| 47 | + void_foo_constchar = ext.void_foo_constchar |
| 48 | + void_foo_constchar.argtypes = [ctypes.c_char_p] |
| 49 | + void_foo_constchar.restype = None |
| 50 | + |
| 51 | + return benchmark(loops) |
| 52 | + |
| 53 | + |
| 54 | +def benchmark(loops): |
| 55 | + void_foo_void = ext.void_foo_void |
| 56 | + int_foo_int = ext.int_foo_int |
| 57 | + void_foo_int = ext.void_foo_int |
| 58 | + void_foo_int_int = ext.void_foo_int_int |
| 59 | + void_foo_int_int_int = ext.void_foo_int_int_int |
| 60 | + void_foo_int_int_int_int = ext.void_foo_int_int_int_int |
| 61 | + void_foo_constchar = ext.void_foo_constchar |
| 62 | + |
| 63 | + range_it = range(loops) |
| 64 | + |
| 65 | + # Test calling the functions using the implied arguments mechanism |
| 66 | + t0 = pyperf.perf_counter() |
| 67 | + |
| 68 | + for _ in range_it: |
| 69 | + void_foo_void() |
| 70 | + int_foo_int(1) |
| 71 | + void_foo_int(1) |
| 72 | + void_foo_int_int(1, 2) |
| 73 | + void_foo_int_int_int(1, 2, 3) |
| 74 | + void_foo_int_int_int_int(1, 2, 3, 4) |
| 75 | + void_foo_constchar(b"bytes") |
| 76 | + |
| 77 | + return pyperf.perf_counter() - t0 |
| 78 | + |
| 79 | + |
| 80 | +def add_cmdline_args(cmd, args): |
| 81 | + if args.argtypes: |
| 82 | + cmd.append("--argtypes") |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + runner = pyperf.Runner(add_cmdline_args=add_cmdline_args) |
| 87 | + runner.metadata["description"] = "ctypes function call overhead benchmark" |
| 88 | + |
| 89 | + runner.argparser.add_argument("--argtypes", action="store_true") |
| 90 | + options = runner.parse_args() |
| 91 | + |
| 92 | + if options.argtypes: |
| 93 | + runner.bench_time_func("ctypes_argtypes", benchmark_argtypes) |
| 94 | + else: |
| 95 | + runner.bench_time_func("ctypes", benchmark) |
0 commit comments