benchmark

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Wed Aug 6 21:38:53 EDT 2008


On Aug 7, 2:05 am, "Jack" <nos... at invalid.com> 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:
>
> http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-p...

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
        self.next = None
    def shout(self, shout, deadif):
        if shout < deadif:
            return shout + 1
        self.prev.next = self.next
        self.next.prev = 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:
                last.next = current
                current.prev = last
            last = current
        self.first.prev = last
        last.next = self.first
    def kill(self, nth):
        current = self.first
        shout = 1
        while current.next != current:
            shout = current.shout(shout, nth)
            current = current.next
        self.first = current
        return current


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

psyco.full()
main()

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).

Bye,
bearophile



More information about the Python-list mailing list