Microthreads

Chris Liechti cliechti at gmx.net
Sat May 17 18:47:30 EDT 2003


"Gary Richardson" <garyr at fidalgo.net> wrote in 
news:vcd8g1jd2ql80 at corp.supernews.com:

> In David Mertz's article on microthreads
> http://www-106.ibm.com/developerworks/linux/library/l-pythrd.html
> the script in Listing 2 (appended below) attempts to characterize the
> overhead of his scheme. It creates 100,000 threads and measures the time
> to perform one million context switches. On my computer (400 MHz AMD-K6,
> Win98 SE, 192 MB ram) this took about 40 seconds. The article states that
> on a 366 MHz Pentium II laptop the time was about 10 seconds. I'm using
> Python 2.2, Pythonwin, win32all build 146.
> Is my computer really sick?

yes ;-)

1.7s on my P4 2.5GHz

that is roughly:
>>> 1.7*2500/400
10.625
seconds on a 400Mhz machine.

make sure you run the program more than once, and look that no other 
resource havy program is running.

chris



> from __future__ import generators
> 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
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 



-- 
Chris <cliechti at gmx.net>





More information about the Python-list mailing list