Ken*_*n L 7 python command-line interactive
我在一个小函数上使用Enthought EPD-Free 7.3-1,当我剪切/粘贴到交互式会话(PyLab)并运行它时,它只需不到一秒钟.当我从命令行" python probtest.py" 运行相同的代码时,它需要超过16秒.  
我已经确认两者都在使用相同的python环境.也许相关(也许不是)但是在.py文件目录中,没有.pyc文件......我最近做过的python脚本都没有关联的.pyc文件.我检查了文件夹的读/写权限,使用了"修复权限"(Mac OSX-Lion),并卸载/重新安装了EPD_Free python,但没有运气.  
我不知道可能是什么原因.我正在使用的代码(x个骰子的简单测试,寻找至少y六个):
import numpy as np
import sys
def runTest(numDice, numSixes, numThrows = 10000):
    nSuccess = 0
    for i in range(numThrows):
        dList = np.random.randint(1,7,numDice)
        if sum(dList==6) >= numSixes:
            nSuccess += 1
    return float(nSuccess)/numThrows
print runTest(900,150,5000)
print sys.version 
Run Code Online (Sandbox Code Playgroud)
有关为什么命令行python速度慢得多的想法?提前致谢.
啊,这个似乎很熟悉.如果你正在使用pylab接口,它可能会将numpy sum导入范围,覆盖内置.numpy的总和会快得多(以下两个代码之间的唯一区别就是我添加from numpy import sum到第二个代码中):
localhost-2:coding $ time python sumtime.py 
0.5106
2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
real    0m20.313s
user    0m19.955s
sys 0m0.247s
localhost-2:coding $ time python sumtime_with_np_sum.py 
0.5118
2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
real    0m0.572s
user    0m0.345s
sys 0m0.220s
Run Code Online (Sandbox Code Playgroud)
您可以通过检查是否验证来验证这一点sum is np.sum.