File tree 3 files changed +43
-0
lines changed
3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -1375,6 +1375,11 @@ unpacking_collector<policy> collect_arguments(Args &&...args) {
1375
1375
template <typename Derived>
1376
1376
template <return_value_policy policy, typename ... Args>
1377
1377
object object_api<Derived>::operator ()(Args &&...args) const {
1378
+ #if !defined(NDEBUG) && PY_VERSION_HEX >= 0x03060000
1379
+ if (!PyGILState_Check ()) {
1380
+ pybind11_fail (" pybind11::object_api<>::operator() PyGILState_Check() failure." );
1381
+ }
1382
+ #endif
1378
1383
return detail::collect_arguments<policy>(std::forward<Args>(args)...).call (derived ().ptr ());
1379
1384
}
1380
1385
Original file line number Diff line number Diff line change @@ -172,4 +172,10 @@ TEST_SUBMODULE(callbacks, m) {
172
172
for (auto i : work)
173
173
start_f (py::cast<int >(i));
174
174
});
175
+
176
+ m.def (" callback_num_times" , [](py::function f, std::size_t num) {
177
+ for (std::size_t i = 0 ; i < num; i++) {
178
+ f ();
179
+ }
180
+ });
175
181
}
Original file line number Diff line number Diff line change 2
2
import pytest
3
3
from pybind11_tests import callbacks as m
4
4
from threading import Thread
5
+ import time
5
6
6
7
7
8
def test_callbacks ():
@@ -146,3 +147,34 @@ def test_async_async_callbacks():
146
147
t = Thread (target = test_async_callbacks )
147
148
t .start ()
148
149
t .join ()
150
+
151
+
152
+ def test_callback_num_times ():
153
+ # Super-simple micro-benchmarking related to PR #2919.
154
+ # Example runtimes (Intel Xeon 2.2GHz, fully optimized):
155
+ # num_millions 1, repeats 2: 0.1 secs
156
+ # num_millions 20, repeats 10: 11.5 secs
157
+ one_million = 1000000
158
+ num_millions = 1 # Try 20 for actual micro-benchmarking.
159
+ repeats = 2 # Try 10.
160
+ rates = []
161
+ for rep in range (repeats ):
162
+ t0 = time .time ()
163
+ m .callback_num_times (lambda : None , num_millions * one_million )
164
+ td = time .time () - t0
165
+ rate = num_millions / td if td else 0
166
+ rates .append (rate )
167
+ if not rep :
168
+ print ()
169
+ print (
170
+ "callback_num_times: {:d} million / {:.3f} seconds = {:.3f} million / second" .format (
171
+ num_millions , td , rate
172
+ )
173
+ )
174
+ if len (rates ) > 1 :
175
+ print ("Min Mean Max" )
176
+ print (
177
+ "{:6.3f} {:6.3f} {:6.3f}" .format (
178
+ min (rates ), sum (rates ) / len (rates ), max (rates )
179
+ )
180
+ )
You can’t perform that action at this time.
0 commit comments