14
14
ext = ctypes .cdll .LoadLibrary (spec .origin )
15
15
16
16
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 ):
18
55
void_foo_void = ext .void_foo_void
19
56
int_foo_int = ext .int_foo_int
20
57
void_foo_int = ext .void_foo_int
@@ -24,8 +61,9 @@ def benchmark(n):
24
61
void_foo_constchar = ext .void_foo_constchar
25
62
26
63
# Test calling the functions using the implied arguments mechanism
64
+ t0 = pyperf .perf_counter ()
27
65
28
- for i in range (n ):
66
+ for i in range (loops ):
29
67
void_foo_void ()
30
68
int_foo_int (1 )
31
69
void_foo_int (1 )
@@ -34,9 +72,33 @@ def benchmark(n):
34
72
void_foo_int_int_int_int (1 , 2 , 3 , 4 )
35
73
void_foo_constchar (b"bytes" )
36
74
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
+
37
92
38
93
if __name__ == "__main__" :
39
94
runner = pyperf .Runner ()
40
95
runner .metadata ["description" ] = "ctypes function call overhead benchmark"
41
96
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