# Why is Python slow?

I tried replying to this post from Peter Bowyer, but the comment submit form was behind an httpd-authenticated wall, so I figured I'ld post the reply here instead.

Have you tried posting to the Python Tutor list (tutor@python.org), and
asking them why your code is so slow? You'll probably get some interesting
responses.
A couple of things I've noticed off the top:
You could replace this
`x = 0`

with this

bins = []

for x in range(MAXSTEPS): bins.append(0)`bins = [0 for x in xrange(MAXSTEPS)]`

which should be faster for a couple of reasons. First, list comprehensions
are faster than repeated calls to append (I believe). Second, xrange
should be faster than range, because it just returns the numbers one at a
time instead of creating the whole list at once.

Here's some code showing how much faster that one change is:
`>>> t1="""x=0`

... bins=[]

... for x in xrange(20): bins.append(0)"""

>>> t2 = """bins = [0 for x in xrange(20)]"""

>>> time1 = timeit.Timer(t1)

>>> time2 = timeit.Timer(t2)

>>> time1.timeit()

10.322476353976072

>>> time2.timeit()

7.6572002255583129

As a side note, I ran:
`>>> t3 = """bins = [0] * 300"""`

which takes half the time of t2 to do 15 times as many entries...
Interesting. I'll update this with the results of the other tests as they
finish running...

>>> time1 = timeit.Timer(t1)

>>> time3.timeit()

3.0881361995940324

Okay, another thought. You calculate the distance every time through the inner loop, which seems really slow. Perhaps you could keep track of the distance, and update it in the call to walk?

Update:

Here are the results from running all of them for 300 iterations.
`>>> t1="""x=0`

... bins=[]

... for x in xrange(300): bins.append(0)"""

>>> t2 = """bins = [0 for x in xrange(300)]"""

>>> t3 = """bins = [0] * 300"""

>>> time1 = timeit.Timer(t1)

>>> time2 = timeit.Timer(t2)

>>> time3 = timeit.Timer(t3)

>>> time1.timeit()

144.48788944637977

>>> time2.timeit()

105.76589055161526

>>> time3.timeit()

3.0881361995940324