I decided to find out how multithreading is done in Python, and I did a comparison to find out what kind of performance enhancement I would get on a dual core processor. I found that my simple multi-threaded code is slower than the sequential equivalent, and I cannot understand why.
The test I developed was to generate a large list of random numbers and then print the maximum
from random import random
import threading
def ox():
print max([random() for x in xrange(20000000)])
ox()It takes about 6 seconds to complete my Intel Core 2 Duo, and it ox();ox()takes about 12 seconds.
Then I tried calling ox () from two threads to find out how quickly this would complete.
def go():
r = threading.Thread(target=ox)
r.start()
ox()
go()it takes about 18 seconds to complete, with two results being printed within 1 second of each other. Why should it be slower?
, ox() , Windows ox() python, 75% , . Python , max(), ?