A curious bit of code...

Ethan Furman ethan at stoneleaf.us
Thu Feb 13 20:59:10 CET 2014


On 02/13/2014 11:43 AM, Peter Otten wrote:
> 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.

For completeness:

# the slowest method from Peter
$ python -m timeit -s 's = "<alpha>"' 's.startswith("<") and s.endswith(">")'
1000000 loops, best of 3: 0.309 usec per loop

# the re method from Roy
$ python -m timeit -s "import re;pattern=re.compile(r'^<.*>$');s = '<alpha>'" "pattern.match(s)"
1000000 loops, best of 3: 0.466 usec per loop

--
~Ethan~



More information about the Python-list mailing list