简体   繁体   中英

Why does python3 take twice as long as python2 to load?

I'm supporting a project on both python2 and python3, and noticed that the python3 interpreter is taking almost 3 times as long to load as the python2 interpreter.

(Running on Arch Linux)

$ cat hello.py
print('Hello World!')
$ python -V
Python 3.4.2
$ time python hello.py
Hello World!

real    0m1.561s
user    0m1.290s
sys     0m0.110s
$ python2 -V
Python 2.7.9
$ time python2 hello.py
Hello World!

real    0m0.613s
user    0m0.513s
sys     0m0.070s

And once more with no code:

$ rm foo ; touch foo ; time python foo ; time python2 foo

real    0m1.710s
user    0m1.297s
sys     0m0.103s

real    0m1.040s
user    0m0.667s
sys     0m0.100s
$ echo $PYTHONSTARTUP

$ rm foo ; touch foo ; time python -B foo ; time python2 -B foo

real    0m1.554s
user    0m1.117s
sys     0m0.123s

real    0m0.678s
user    0m0.557s
sys     0m0.090s

What is going on here?

Try disabling " local site libraries " by passing -S to python. Passing this has sped things up noticeably for me when I've been playing with slower ARM boxes. For example, I get:

time python3 -S -c pass

running in about 0.076 seconds, wall clock time, while a vanilla:

time python3 -c pass

takes 0.247 seconds. Passing this options to python2 results in a similar speedup for me under various distributions, so even this doesn't explain the disparity—just a way to work around some of it!

Hope you don't mind me resurrecting this thread; google pointed me at this and I wanted to get a useful answer.

The startup time you're seeing is significantly different to what I can see on my system:

$ touch py
$ python3 py
$ time python3 py

real    0m0.027s
user    0m0.019s
sys 0m0.006s

$ python3 -V
Python 3.4.2

$ time python py

real    0m0.023s
user    0m0.013s
sys 0m0.008s

$ python -V
Python 2.7.8

I see a marginal increase with python3, but nothing like the 1 second increase that you are seeing. There are probably 2 reasons that I can think of that may be causing the difference. The first is that you may have a python startup file executing on interpreter load.

$ echo $PYTHONSTARTUP
~/.pythonrc

Check to see if you have a startup file loaded for python3 but not for python2.

Also, try running your test without writing bytecode:

time python -B foo

This is probably an environment issue local to your setup rather than a major difference between versions of the interpreter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM