Skip to content

Commit

Permalink
Use bench_time_func, use a single python script
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom committed May 24, 2022
1 parent 1951e10 commit 621ea37
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ dynamic = ["version"]
[tool.pyperformance]
name = "ctypes_argtypes"
tags = "extension"
runscript = "run_benchmark_argtypes.py"
extra_args = "--argtypes"
setup_py = true
68 changes: 65 additions & 3 deletions pyperformance/data-files/benchmarks/bm_ctypes/run_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,44 @@
ext = ctypes.cdll.LoadLibrary(spec.origin)


def benchmark(n):
def benchmark_argtypes(loops):
void_foo_void = ext.void_foo_void
void_foo_void.argtypes = []
void_foo_void.restype = None

int_foo_int = ext.void_foo_int
int_foo_int.argtypes = [ctypes.c_int]
int_foo_int.restype = ctypes.c_int

void_foo_int = ext.void_foo_int
void_foo_int.argtypes = [ctypes.c_int]
void_foo_int.restype = None

void_foo_int_int = ext.void_foo_int_int
void_foo_int_int.argtypes = [ctypes.c_int, ctypes.c_int]
void_foo_int_int.restype = None

void_foo_int_int_int = ext.void_foo_int_int_int
void_foo_int_int_int.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int]
void_foo_int_int_int.restype = None

void_foo_int_int_int_int = ext.void_foo_int_int_int_int
void_foo_int_int_int_int.argtypes = [
ctypes.c_int,
ctypes.c_int,
ctypes.c_int,
ctypes.c_int,
]
void_foo_int_int_int_int.restype = None

void_foo_constchar = ext.void_foo_constchar
void_foo_constchar.argtypes = [ctypes.c_char_p]
void_foo_constchar.restype = None

return benchmark(loops)


def benchmark(loops):
void_foo_void = ext.void_foo_void
int_foo_int = ext.int_foo_int
void_foo_int = ext.void_foo_int
Expand All @@ -24,8 +61,9 @@ def benchmark(n):
void_foo_constchar = ext.void_foo_constchar

# Test calling the functions using the implied arguments mechanism
t0 = pyperf.perf_counter()

for i in range(n):
for i in range(loops):
void_foo_void()
int_foo_int(1)
void_foo_int(1)
Expand All @@ -34,9 +72,33 @@ def benchmark(n):
void_foo_int_int_int_int(1, 2, 3, 4)
void_foo_constchar(b"bytes")

void_foo_void()
int_foo_int(1)
void_foo_int(1)
void_foo_int_int(1, 2)
void_foo_int_int_int(1, 2, 3)
void_foo_int_int_int_int(1, 2, 3, 4)
void_foo_constchar(b"bytes")

void_foo_void()
int_foo_int(1)
void_foo_int(1)
void_foo_int_int(1, 2)
void_foo_int_int_int(1, 2, 3)
void_foo_int_int_int_int(1, 2, 3, 4)

return pyperf.perf_counter() - t0


if __name__ == "__main__":
runner = pyperf.Runner()
runner.metadata["description"] = "ctypes function call overhead benchmark"

runner.bench_func("ctypes", benchmark, 1000000)
runner.argparser.add_argument("--argtypes")
options = runner.parse_args()

if options.argtypes:
runner.bench_time_func("ctypes_argtypes", benchmark_argtypes, inner_loops=3)
else:
runner.bench_time_func("ctypes", benchmark, inner_loops=3)

This file was deleted.

0 comments on commit 621ea37

Please sign in to comment.