using threads with for-loops

Peter Hansen peter at engcorp.com
Tue Sep 28 18:51:46 CEST 2004


Klaus Neuner wrote:
> Hello,
> 
> I wrote a program that does essentially the following:
> 
> for rule in rules:
> 	for line in line_list:
> 		line = my_apply(rule, line)
> 
> line_list contains the lines of some input text.
> 
> To "apply a rule" always means to
> 
>   1. do some regex matches on line
>   2. substitute something in line
> 
> My question is: Given this "architecture", does it make sense
> to use threads? And if so, how?

The code is (based on what you give above) "CPU bound",
which means you will not see any advantage in using
threads.  Threads don't magically make anything go
faster, and in fact have a certain overhead for the context
switch, so no, it makes no sense to use threads here.

-Peter



More information about the Python-list mailing list