Python 2.4:普通脚本中的导入速度与 execfiled 脚本中的导入速度

发布于 2024-10-09 06:20:49 字数 1157 浏览 0 评论 0原文

我刚刚偶然发现了一些对我来说毫无意义的事情。在我工作的地方,我们有许多 Python CGI 网页(只是一个简单的 Apache 服务器设置,不运行 Django / Turbogears 等),我对脚本运行所需的时间感到有点沮丧。我放弃了很多 time.time() 调用,并认为我已将瓶颈确定为 sqlalchemy 的导入(尽管我现在认为它可能是“任何大模块”,因此 sqlalchemy 标签可能放错了位置)。

因此,在尝试了各种不同的操作之后,我最终得到了这个示例,(假设该文件名为“test.py”)

#!/usr/bin/python

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

如果我在命令提示符下运行 test.py(通过将其设置为可执行文件),它通常会显示大约 0.7 秒( +/- 0.1 秒)对于该导入语句。

但是,如果我调用,

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

我的速度会提高大约 10 倍

所以我想我应该用一个小的 tcsh 脚本来包装我的一些 python CGI 脚本,该脚本会调用

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

,并且我通常会得到大约 2-3 倍的加速,而且重要的是,返回的数据仍然是正确的。

使用占用大量 cpu 的脚本而不是占用大量导入的脚本,例如:

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

使用 execfile 时速度会稍微变慢,这是我从 execfile 的轻微开销中所期望的。

有谁知道这是怎么回事?任何人都可以重现类似的速度差异,或者我的设置是否以 execfile 以某种方式修复的方式被破坏?我认为导入在 execfile 中的行为略有不同(或者至少,一旦离开 execfile 语句就不一定可见),但我对速度上如此大的差异感到惊讶。

我在 Oracle 提供的“企业 Linux 服务器版本 5 (Carthage)”上运行 python 2.4 64 位。

I've just stumbled across something 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)".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海夕 2024-10-16 06:20:49

我的猜测是没有真正的区别。只是看起来差别很大。尝试像这样测试它以确保:

# 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文