The python interactive shell is 16 times faster than the command line - what happened?

I use Enthought EPD-Free 7.3-1 for a small function, and when I cut / paste into an interactive session (PyLab) and start it, it takes less than a second. When I run the same code from the python probtest.py command line, it takes more than 16 seconds.

I have confirmed that both use the same python environment. Perhaps related (maybe not), but there is no .pyc file in the directory with the .py file ... none of the python scripts I wrote associated the .pyc files. I checked read / write permissions for the folder, used “repair permissions” (Mac OSX-Lion), and uninstalled / reinstalled EPD_Free python, but no luck.

I do not understand what could be the reason. The code I'm using (simple test x number of dice looking for at least sixteen):

 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 

Any thoughts on why the python command line is much slower? Thanks in advance.

+7
source share
2 answers

Ah, that sounds familiar. If you use the pylab interface, it probably imported the numpy sum into scope, overriding the built-in one. The numpy sum will be much faster (the only difference between the following two codes is that I added from numpy import sum to the second):

 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 

You can check this by checking if sum is np.sum .

+9
source

We discovered this problem with IronPython. The command line imported many default items for each call. And the interactive shell downloaded them once and was ready to go!

So, check which import operations are performed by default and removed from time.

+3
source

All Articles