Skip to content

Instantly share code, notes, and snippets.

@methane
Created June 9, 2017 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save methane/74f6ad95fd6c6ad4dbb32e35012773bf to your computer and use it in GitHub Desktop.
Save methane/74f6ad95fd6c6ad4dbb32e35012773bf to your computer and use it in GitHub Desktop.
python import profile patch
diff --git a/Python/import.c b/Python/import.c
index 9a78d6adc7..d1f8b1e4d6 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -1584,6 +1584,13 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
}
}
else {
+ static int import_level;
+ struct timespec t1, t2;
+
+ clock_gettime(CLOCK_MONOTONIC, &t1);
+ import_level++;
+ //fprintf(stderr, "%*s+ %s\n", import_level, "", PyUnicode_AsUTF8(abs_name));
+
#ifdef WITH_THREAD
_PyImport_AcquireLock();
#endif
@@ -1591,6 +1598,20 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
mod = _PyObject_CallMethodIdObjArgs(interp->importlib,
&PyId__find_and_load, abs_name,
interp->import_func, NULL);
+
+ clock_gettime(CLOCK_MONOTONIC, &t2);
+ if (t1.tv_nsec > t2.tv_nsec) {
+ t2.tv_sec--;
+ t2.tv_nsec += 1000000000;
+ }
+ t2.tv_sec -= t1.tv_sec;
+ t2.tv_nsec -= t1.tv_nsec;
+ fprintf(stderr, "%*s- %s %ld.%09ld\n",
+ import_level, "",
+ PyUnicode_AsUTF8(abs_name),
+ t2.tv_sec, t2.tv_nsec);
+ import_level--;
+
if (mod == NULL) {
goto error;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment