1414ext = ctypes .cdll .LoadLibrary (spec .origin )
1515
1616
17- def benchmark (n ):
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 ):
1855 void_foo_void = ext .void_foo_void
1956 int_foo_int = ext .int_foo_int
2057 void_foo_int = ext .void_foo_int
@@ -24,8 +61,9 @@ def benchmark(n):
2461 void_foo_constchar = ext .void_foo_constchar
2562
2663 # Test calling the functions using the implied arguments mechanism
64+ t0 = pyperf .perf_counter ()
2765
28- for i in range (n ):
66+ for i in range (loops ):
2967 void_foo_void ()
3068 int_foo_int (1 )
3169 void_foo_int (1 )
@@ -34,9 +72,33 @@ def benchmark(n):
3472 void_foo_int_int_int_int (1 , 2 , 3 , 4 )
3573 void_foo_constchar (b"bytes" )
3674
75+ void_foo_void ()
76+ int_foo_int (1 )
77+ void_foo_int (1 )
78+ void_foo_int_int (1 , 2 )
79+ void_foo_int_int_int (1 , 2 , 3 )
80+ void_foo_int_int_int_int (1 , 2 , 3 , 4 )
81+ void_foo_constchar (b"bytes" )
82+
83+ void_foo_void ()
84+ int_foo_int (1 )
85+ void_foo_int (1 )
86+ void_foo_int_int (1 , 2 )
87+ void_foo_int_int_int (1 , 2 , 3 )
88+ void_foo_int_int_int_int (1 , 2 , 3 , 4 )
89+
90+ return pyperf .perf_counter () - t0
91+
3792
3893if __name__ == "__main__" :
3994 runner = pyperf .Runner ()
4095 runner .metadata ["description" ] = "ctypes function call overhead benchmark"
4196
42- runner .bench_func ("ctypes" , benchmark , 1000000 )
97+ runner .argparser .add_argument ("--argtypes" )
98+ options = runner .parse_args ()
99+
100+ if options .argtypes :
101+ runner .bench_time_func ("ctypes_argtypes" , benchmark_argtypes , inner_loops = 3 )
102+ else :
103+ runner .bench_time_func ("ctypes" , benchmark , inner_loops = 3 )
104+
0 commit comments