Killing a running thread
Robert Roy
rjroy at takingcontrol.com
Thu May 2 18:35:27 EDT 2002
On Thu, 2 May 2002 19:21:58 +0000, Alexander Skwar
<ASkwar at DigitalProjects.com> wrote:
>=BBMichael Gilfix=AB sagte am 2002-05-02 um 15:00:17 -0400 :
>> Just write your own map that's threadable, i.e, applies a function
>> to each argument in the list and appends it. Should be trivial to
>> write and you can check the threaded argument.
>
>Wouldn't this be way slower than the built in map?
>
It will be slower, but whether or not it makes a difference in the
grand scheme of things depends on what you are doing in the loop. For
instance if your function is doing something like x +=1 then map will
be a timesaver. But if you are applying a big complicated regular
expression to each item in the list, perhaps the overhead of an
explicit loop is acceptable.
For instance in the following code: 1 000 000 iterations
# apply the regular expression
bash-2.02$ python loops.py
25.6745684658
25.2741594343
# pass
bash-2.02$ python loops.py
3.78650498092
2.29407697603
arg += 1
bash-2.02$ python loops.py
4.3369769461
3.22872198139
#######
If I perform a complex operation, the loop overhead is negligible.
This is where you have to make the design tradeoff. Is it worth 2
extra seconds on a full run to be able to break out of the loop
cleanly?
import time
import re
r = re.compile('aabb')
l = range(1000000)
def test(arg):
r.findall('passababababbababababbbbbabaababaaaaababbabababaaaababbabababababababaaaaabbb')
# pass
# arg += 1
start = time.clock()
for item in l:
test(item)
end = time.clock()
print end-start
accum = 0
start = time.clock()
map(test, l)
end = time.clock()
print end-start
More information about the Python-list
mailing list