bearophileHUGS at bearophileHUGS at
Thu Aug 7 03:38:53 CEST 2008

On Aug 7, 2:05 am, "Jack" <nos... at> wrote:
> I know one benchmark doesn't mean much but it's still disappointing to see
> Python as one of the slowest languages in the test:

That Python code is bad, it contains range() instead of xrange, the
big loop is in the main code instead of inside a function, uses ==
None, etc. That person can try this (with Psyco), I have changed very
little, the code is essentially the same:

import time, psyco
from psyco.classes import __metaclass__

class Person:
    def __init__(self, count):
        self.count = count
        self.prev = None = None
    def shout(self, shout, deadif):
        if shout < deadif:
            return shout + 1 = = self.prev
        return 1

class Chain:
    def __init__(self, size):
        self.first = None
        last = None
        for i in xrange(size):
            current = Person(i)
            if self.first is None:
                self.first = current
            if last is not None:
       = current
                current.prev = last
            last = current
        self.first.prev = last = self.first
    def kill(self, nth):
        current = self.first
        shout = 1
        while != current:
            shout = current.shout(shout, nth)
            current =
        self.first = current
        return current

def main():
    ITER = 100000
    start = time.time()
    for i in xrange(ITER):
        chain = Chain(40)
    end = time.time()
    print 'Time per iteration = %s microseconds ' % ((end - start) *
1000000 / ITER)


us = microseconds
On my PC (that seems similar to his one) this version needs about 38.9
us/iter instead of 189.

On my PC the Java version takes 1.17 us, while the C++ version (with
MinGW 4.2.1) takes 9.8 us.
A raw D translation needs 14.34 us, while a cleaned up (that uses
structs, no getters/setters) needs 4.67 us.
I don't know why my C++ is so much slow (doing the same things to the C
++ version doesn't change its running time much).


More information about the Python-list mailing list