MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / PyTime_PerfCounter

Function PyTime_PerfCounter

asyncpg/protocol/record/pythoncapi_compat.h:1141–1208  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1139{ return _PyTime_GetSystemClockWithInfo(result, NULL); }
1140
1141static inline int PyTime_PerfCounter(PyTime_t *result)
1142{
1143#if PY_VERSION_HEX >= 0x03070000 && !defined(PYPY_VERSION)
1144 return _PyTime_GetPerfCounterWithInfo(result, NULL);
1145#elif PY_VERSION_HEX >= 0x03070000
1146 // Call time.perf_counter_ns() and convert Python int object to PyTime_t.
1147 // Cache time.perf_counter_ns() function for best performance.
1148 static PyObject *func = NULL;
1149 if (func == NULL) {
1150 PyObject *mod = PyImport_ImportModule("time");
1151 if (mod == NULL) {
1152 return -1;
1153 }
1154
1155 func = PyObject_GetAttrString(mod, "perf_counter_ns");
1156 Py_DECREF(mod);
1157 if (func == NULL) {
1158 return -1;
1159 }
1160 }
1161
1162 PyObject *res = PyObject_CallNoArgs(func);
1163 if (res == NULL) {
1164 return -1;
1165 }
1166 long long value = PyLong_AsLongLong(res);
1167 Py_DECREF(res);
1168
1169 if (value == -1 && PyErr_Occurred()) {
1170 return -1;
1171 }
1172
1173 Py_BUILD_ASSERT(sizeof(value) >= sizeof(PyTime_t));
1174 *result = (PyTime_t)value;
1175 return 0;
1176#else
1177 // Call time.perf_counter() and convert C double to PyTime_t.
1178 // Cache time.perf_counter() function for best performance.
1179 static PyObject *func = NULL;
1180 if (func == NULL) {
1181 PyObject *mod = PyImport_ImportModule("time");
1182 if (mod == NULL) {
1183 return -1;
1184 }
1185
1186 func = PyObject_GetAttrString(mod, "perf_counter");
1187 Py_DECREF(mod);
1188 if (func == NULL) {
1189 return -1;
1190 }
1191 }
1192
1193 PyObject *res = PyObject_CallNoArgs(func);
1194 if (res == NULL) {
1195 return -1;
1196 }
1197 double d = PyFloat_AsDouble(res);
1198 Py_DECREF(res);

Callers

nothing calls this directly

Calls 1

PyObject_CallNoArgsFunction · 0.85

Tested by

no test coverage detected