Multi-threading on Multi-CPU machines

Garry Taylor gtaylor at lowebroadway.com
Mon Jul 8 11:02:54 EDT 2002


Hello,
I am attempting to make a multi-threading function in one of my
programs in an effort to gain a speed increase, but I'm getting quite
the opposite, even on a dual-CPU Intel/Linux box. Can anyone enlighten
me as to why, my code is below:
--------
import thread
import time

ThreadCounter = 0
Iterations = 100

def Threader():
	global ThreadCounter
	global Iterations
	Counter = 0
	temptime = time.time()
	while Counter < Iterations:
		Counter = Counter + 1
		thread.start_new_thread(TakesTime,())
		
	while ThreadCounter < Iterations:
		pass

	print "Threaded: "+str(time.time() - temptime)

def TakesTime():
	global ThreadCounter
	Text = "Test"
	Counter = 0
	while Counter < 20:
		Text = Text + Text
		Counter = Counter + 1
 	ThreadCounter = ThreadCounter + 1
	
def NoThreader():
	global Iterations
	temptime = time.time()
	Counter = 0
	while Counter < Iterations:
		Counter = Counter + 1
		TakesTime()
	
	print "Non-Threaded: "+str(time.time() - temptime)

Threader()
NoThreader()
--------

This does the same thing, threaded and then not, but on all of my
machinbes, the multi-threaded is slower, what can I do about this?

Thanks

garry



More information about the Python-list mailing list