|
| 1 | +""" |
| 2 | +Test the function call overhead of ctypes with specified arguments. |
| 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(n): |
| 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 | + # Test calling the functions using the specified arguments mechanism |
| 52 | + |
| 53 | + for i in range(n): |
| 54 | + void_foo_void() |
| 55 | + int_foo_int(1) |
| 56 | + void_foo_int(1) |
| 57 | + void_foo_int_int(1, 2) |
| 58 | + void_foo_int_int_int(1, 2, 3) |
| 59 | + void_foo_int_int_int_int(1, 2, 3, 4) |
| 60 | + void_foo_constchar(b"bytes") |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + runner = pyperf.Runner() |
| 65 | + runner.metadata[ |
| 66 | + "description" |
| 67 | + ] = "ctypes function call overhead, with specified arguments, benchmark" |
| 68 | + |
| 69 | + runner.bench_func("ctypes", benchmark, 1000000) |
0 commit comments