开发者

Python 2.4: Speed of imports in normal script vs imports in execfile'd script

I've just stumbled across somethin开发者_开发百科g that makes no sense to me. Where I work, we have a number of Python CGI webpages (just a simple Apache server setup, not running Django / Turbogears or the like) and I've been getting a bit frustrated with how long it takes the scripts to run. I chucked lots of time.time() calls and thought I'd identified the bottleneck as the import of sqlalchemy (though I now think it's probably "any big module" so the sqlalchemy tag is perhaps misplaced).

So, after trying various different things, I ended up with this example, (assume the file's called 'test.py')

#!/usr/bin/python

import time
t1 = time.time()
import sqlalchemy
print time.time() - t1

If I run test.py at the command prompt (by setting it executable), it typically shows about 0.7 seconds (+/- 0.1 seconds) for that import statement.

But, if I call

python -c "execfile('test.py')"

I get a speed up of about a factor of 10

So I thought I'd wrap some of my python CGI scripts with a little tcsh script that calls

python -c "execfile('mypythoncgiscript.py')"

and I get speed-ups typically about a factor of 2-3, and, importantly, the data returned is still correct.

With a cpu-heavy rather than import-heavy script, e.g:

t1 = time().time()
a = 0
for i in xrange(10000000):
    a += 1
print time.time() - t1

I get a very slight slowdown using execfile, which is what I would have expected from the slight execfile overhead.

Does anyone know what's going on here? Can anyone reproduce similar speed differences or is my setup broken in a way that execfile somehow fixes? I thought imports behaved slightly differently within execfile (or at least, aren't necessarily visible once you've left the execfile statement) but I'm surprised by such a large difference in speed.

I'm running python 2.4 64bit on Oracle-supplied "Enterprise Linux Server release 5 (Carthage)".


My guess is that there is no real difference. It only looks like a big difference. Try and test it like this to be sure:

# time python test.py
0.0514879226685
python test.py  0.06s user 0.01s system 95% cpu 0.071 total

# time python -c 'execfile
0.0515019893646
python -c 'execfile("test.py")'  0.06s user 0.01s system 95% cpu 0.071 total
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜