Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python interactive shell 16x faster than command line - what's wrong?

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.

like image 227
Ken L Avatar asked Jul 03 '12 04:07

Ken L


2 Answers

Ah, this one seems familiar. If you're using a pylab interface, it's probably imported the numpy sum into scope, overriding the builtin. numpy's sum will be much faster (the only difference between the following two codes is that I've 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 verify this by checking to see if sum is np.sum.

like image 80
DSM Avatar answered Oct 24 '22 04:10

DSM


We found this issue with IronPython. The command line was importing lots of items by default for each invocation. And the interactive shell had them loaded once and was ready go!

So check what imports are done by default and removed them from the timing.

like image 44
Preet Sangha Avatar answered Oct 24 '22 05:10

Preet Sangha