doubling the number of tests, but not taking twice as long

Peter Otten __peter__ at web.de
Tue Jul 17 02:59:22 EDT 2018


Larry Martell wrote:

> I had some code that did this:
> 
> meas_regex = '_M\d+_'
> meas_re = re.compile(meas_regex)
> 
> if meas_re.search(filename):
>     stuff1()
> else:
>     stuff2()
> 
> I then had to change it to this:
> 
> if meas_re.search(filename):
>     if 'MeasDisplay' in filename:
>         stuff1a()
>     else:
>         stuff1()
> else:
>     if 'PatternFov' in filename:
>         stuff2a()
>    else:
>         stuff2()
> 
> This code needs to process many tens of 1000's of files, and it runs
> often, so it needs to run very fast. Needless to say, my change has
> made it take 2x as long. 

That is *not* self-evident. Usually stuffX() would take much longer than the 
initial tests.

So the first step would be to verify that

if meas_re.search(filename):
    if 'MeasDisplay' in filename:
        pass
    else:
        pass
else:
    if 'PatternFov' in filename:
        pass
   else:
        pass

takes a significant amount of the total time the piece of code you give 
takes to execute.

> Can anyone see a way to improve that?

Not really. I'd check if there is a branch that is executed most of the time 
or that takes much longer to execute than the other ones, and then try to 
optimize that.




More information about the Python-list mailing list