"Tim Williams" <[EMAIL PROTECTED]> wrote: > Isn't slicing still faster than startswith? As you mention timeit, > then you should probably add slicing to the pot too :) >
Possibly, but there are so many other factors that affect the timing that writing it clearly should be your first choice. Some timings: @echo off setlocal cd \python25\lib echo "startswith" ..\python timeit.py -s "s='abracadabra1'*1000;t='abracadabra2'" s.startswith(t) ..\python timeit.py -s "s='abracadabra1'*1000;t='abracadabra1'" s.startswith(t) echo "prebound startswith" ..\python timeit.py -s "s='abracadabra1'*1000;t='abracadabra2';startswith=s.startswith" startswith(t) ..\python timeit.py -s "s='abracadabra1'*1000;t='abracadabra1';startswith=s.startswith" startswith(t) echo "slice with len" ..\python timeit.py -s "s='abracadabra1'*1000;t='abracadabra2'" s[:len(t)]==t ..\python timeit.py -s "s='abracadabra1'*1000;t='abracadabra1'" s[:len(t)]==t echo "slice with magic number" ..\python timeit.py -s "s='abracadabra1'*1000;t='abracadabra2'" s[:12]==t ..\python timeit.py -s "s='abracadabra1'*1000;t='abracadabra1'" s[:12]==t and typical output from this is: "startswith" 1000000 loops, best of 3: 0.542 usec per loop 1000000 loops, best of 3: 0.514 usec per loop "prebound startswith" 1000000 loops, best of 3: 0.472 usec per loop 1000000 loops, best of 3: 0.474 usec per loop "slice with len" 1000000 loops, best of 3: 0.501 usec per loop 1000000 loops, best of 3: 0.456 usec per loop "slice with magic number" 1000000 loops, best of 3: 0.34 usec per loop 1000000 loops, best of 3: 0.315 usec per loop So for these particular strings, the naive slice wins if the comparison is true, but loses to the pre-bound method if the comparison fails. The slice is taking a hit from calling len every time, so pre-calculating the length (which should be possible in the same situations as pre-binding startswith) might be worthwhile, but I would still favour using startswith unless I knew the code was time critical. -- http://mail.python.org/mailman/listinfo/python-list