Thread performance on Python 2.6

Rodrick Brown rodrick.brown at gmail.com
Thu Dec 31 00:02:49 EST 2009


I started dabbling with threads in python and for some odd reason the
performance seems extremely poor on my 2 core system.
It this a simplified version spawn 2 threads write some data to a file and
time the results vs doing the same sequentially.
Why is the performance so much slower with the threaded version?


#!/usr/bin/python
import time
from threading import Thread
from tempfile import NamedTemporaryFile

BYTES=10000000
class FileWriterThread(Thread):
    def __init__(self,filenam):
        Thread.__init__(self)
        self.filenam = filenam

    def run(self):
            self.filename = NamedTemporaryFile(delete=False)
        start = time.time()
        for count in xrange(1,BYTES):
            self.filename.write(str(count))
            self.filename.write("\n")
        end = time.time()

        return (end - start)

def fileWriter(bytesToWrite):
    f = NamedTemporaryFile(delete=False)
    start = time.time()
    for n in xrange(1,bytesToWrite):
        f.write(str(n))
        f.write("\n")
    end = time.time()
    return (end - start)

if __name__ == "__main__":
    #tmpfile = NamedTemporaryFile(delete=False)
    total = 0.0
    start = time.time()
    for x in xrange(2):
        c = FileWriterThread(BYTES)
        c.start()
    c.join()
    end = time.time()
    print "Runtime (thread based): %f" % (end - start)
    for x in xrange(2):
        c = fileWriter(BYTES)
        print "Runtime (None thread based): %f" % c
        total += c
    print "Total Runtime (None thread based): %f "  % total


rbrown at laptop:~/code/python$ python filewriter_thr.py
Runtime (thread based): 66.721260
Runtime (None thread based): 16.000052
Runtime (None thread based): 16.078885
Total Runtime (None thread based): 32.078937

rbrown at laptop:~/code/python$ grep ^cpu.*cores /proc/cpuinfo
cpu cores : 2
cpu cores : 2

-- 
[ Rodrick R. Brown ]
http://www.rodrickbrown.com http://www.linkedin.com/in/rodrickbrown
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091231/40636ddd/attachment.html>


More information about the Python-list mailing list