[Python-Dev] Python startup time

Victor Stinner victor.stinner at gmail.com
Thu Jul 20 04:56:43 EDT 2017


I applied the patch above to count the number of times that Python is
run. Running the Python test suite with "./python -m test -j0 -rW"
runs Python 2,256 times.

Honestly, I expected more. I'm running tests with Python compiled in
debug mode. And in debug mode, Python startup time is much worse:

haypo at selma$ python3 -m perf command --inherit=PYTHONPATH -v -- ./python -c pass
command: Mean +- std dev: 46.4 ms +- 2.3 ms

FYI I'm using gcc -O0 rather than -Og to make compilation even faster.


diff --git a/Lib/site.py b/Lib/site.py
index 7dc1b04..4b0c167 100644
--- a/Lib/site.py
+++ b/Lib/site.py
@@ -540,6 +540,21 @@ def execusercustomize():
                 (err.__class__.__name__, err))

+def run_counter():
+    import fcntl
+    fd = os.open("/home/haypo/prog/python/master/run_counter",
+                 os.O_WRONLY | os.O_CREAT | os.O_APPEND)
+    try:
+        fcntl.flock(fd, fcntl.LOCK_EX)
+        try:
+            os.write(fd, b'\x01')
+        finally:
+            fcntl.flock(fd, fcntl.LOCK_UN)
+    finally:
+        os.close(fd)
 def main():
     """Add standard site-specific directories to the module search path.

@@ -568,6 +583,7 @@ def main():
+    run_counter()

 # Prevent extending of sys.path when python was started with -S and
 # site is imported later.

More information about the Python-Dev mailing list