A curious bit of code...

Peter Otten __peter__ at web.de
Thu Feb 13 14:43:58 EST 2014


forman.simon at gmail.com wrote:

> I ran across this and I thought there must be a better way of doing it,
> but then after further consideration I wasn't so sure.
> 
>   if key[:1] + key[-1:] == '<>': ...
> 
> 
> Some possibilities that occurred to me:
> 
>   if key.startswith('<') and key.endswith('>'): ...
> 
> and:
> 
>   if (key[:1], key[-1:]) == ('<', '>'): ...
> 
> 
> I haven't run these through a profiler yet, but it seems like the original
> might be the fastest after all?

$ python -m timeit -s 's = "<alpha>"' 's[:1]+s[-1:] == "<>"'
1000000 loops, best of 3: 0.37 usec per loop

$ python -m timeit -s 's = "<alpha>"' 's[:1] == "<" and s[-1:] == ">"'
1000000 loops, best of 3: 0.329 usec per loop

$ python -m timeit -s 's = "<alpha>"' 's.startswith("<") and 
s.endswith(">")'
1000000 loops, best of 3: 0.713 usec per loop

The first is too clever for my taste.

The second is fast and easy to understand. It might attract "improvements" 
replacing the slice with an index, but I trust you will catch that with your 
unit tests ;)

Personally, I'm willing to spend the few extra milliseconds and use the 
foolproof third.




More information about the Python-list mailing list