[IronPython] Generator Performance (IronPython vs. CPython(

Anthony Tarlano mailinglist.account at gmail.com
Thu Mar 16 22:24:03 CET 2006


Hi,

I investigating the use of  lightweight threads, so I was reading
"Charming Python #b7: Implementing weightless threads with Python
generators. by David Mertz, Ph.D."

The first example microthreads.py is about the simplest weightless
thread scheduler one could choose. Here is the code:

-------------------------------------------------------------------------------------------------------------------
# microthreads.py

import sys, time

threads = []
TOTALSWITCHES = 10**6
NUMTHREADS    = 10**5

def null_factory():
    def empty():
        while 1: yield None
    return empty()

def quitter():
    for n in xrange(TOTALSWITCHES/NUMTHREADS):
        yield None

def scheduler():
    global threads
    try:
        while 1:
            for thread in threads: thread.next()
    except StopIteration:
        pass

if __name__ == "__main__":
    for i in range(NUMTHREADS):
        threads.append(null_factory())
    threads.append(quitter())
    starttime = time.clock()
    scheduler()
    print "TOTAL TIME:    ", time.clock()-starttime
    print "TOTAL SWITCHES:", TOTALSWITCHES
    print "TOTAL THREADS: ", NUMTHREADS

-------------------------------------------------------------------------------------------------------------------

After running the example on CPython the following results are printed:

sh-2.04$ python microthreads.py
TOTAL TIME:     0.718205526121
TOTAL SWITCHES: 1000000
TOTAL THREADS:  100000

After running the example on IronPython the following results are printed:

sh-2.04$ IronPythonConsole microthreads.py
TOTAL TIME:     1.49999237061
TOTAL SWITCHES: 1000000
TOTAL THREADS:  100000

This shows that IronPython took 0.781786844489 more then CPython to do
the same switching between generators in this example. I was quites
surprise to see this since that's more then double the time of
CPython.

Thus, the question is whether this performance result is acceptable to
the IronPython team.

Thanks,

Anthony



More information about the Ironpython-users mailing list