I’m using Enthought EPD-Free 7.3-1 on a small function, and when I cut/paste into an interactive session (PyLab) and run it, it takes less than a second. When I run the same code from the command line “python probtest.py” it takes over 16 seconds.
I’ve confirmed both are using the same python environment. Maybe related (maybe not) but in the directory with the .py file, there is not a .pyc file…none of the python scripts I’ve done recently have associated .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’m at a loss as to what could be the cause. The code I’m using (simple test of x number of dice, looking for at least y sixes):
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 about why the command line python is so much slower? Thanks in advance.
Ah, this one seems familiar. If you’re using a pylab interface, it’s probably imported the numpy
suminto scope, overriding the builtin. numpy’s sum will be much faster (the only difference between the following two codes is that I’ve addedfrom numpy import sumto the second):You can verify this by checking to see if
sum is np.sum.