Re: Thread performance on Python 2.6

2009-12-31 Thread Jan Kaliszewski

31-12-2009 Rodrick Brown rodrick.br...@gmail.com wrote:


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?


I suppose here is the answer: http://www.dabeaz.com/python/GIL.pdf
(anyway -- very interesting material).

AFAIU, in Python 3.2 some of the problems will be corrected (though GIL
itself stays alive) -- see:
http://docs.python.org/dev/py3k/whatsnew/3.2.html#multi-threading

Cheers,
*j
--
http://mail.python.org/mailman/listinfo/python-list


Thread performance on Python 2.6

2009-12-30 Thread Rodrick Brown
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=1000
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


rbr...@laptop:~/code/python$ python filewriter_thr.py
Runtime (thread based): 66.721260
Runtime (None thread based): 16.52
Runtime (None thread based): 16.078885
Total Runtime (None thread based): 32.078937

rbr...@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
-- 
http://mail.python.org/mailman/listinfo/python-list