3 ms·
I reckon this is more comparable to the Ruby version: from random import Random from threading import Thread rand = Random().random THREADS, SIZE = 20,
by astrec 18y ago
I reckon this is more comparable to the Ruby version:
from random import Random
from threading import Thread
rand = Random().random
THREADS, SIZE = 20, 100000
threads = []
class Test(Thread):
def __init__ (self):
Thread.__init__(self)
def run(self):
a = [rand()*SIZE for x in xrange(SIZE)]
a.sort()
def test():
for i in xrange(THREADS):
t = Test()
threads.append(t)
t.start()
for t in threads :
if t.isAlive() :
t.join()
if __name__=='__main__':
from timeit import Timer
t = Timer("test()", "from __main__ import test")
print "%.2f sec/pass" % (t.timeit(number=10)/10)
Which results in 5.48 sec/pass
Looking at cProfile, 50 or so calls to built-in method acquire takes an avg. 4.102 sec.