[Python-ideas] string codes & substring equality

Ethan Furman ethan at stoneleaf.us
Sun Dec 1 21:55:58 CET 2013


On 11/28/2013 04:12 PM, Steven D'Aprano wrote:
>
> And some benchmarks:
>
> py> from timeit import Timer
> py> setup = "from __main__ import match"
> py> t1 = Timer("match('abcdef', 'cde', 2, -1)", setup)
> py> t2 = Timer("s[2:-1] == 'cde'", "s = 'abcdef'")
> py> min(t1.repeat(repeat=5))
> 1.2987589836120605
> py> min(t2.repeat(repeat=5))
> 0.25656223297119141
>
> Slicing is about three times faster.

Wouldn't that be 5 times faster?

I tried to get startswith to be quicker, and compared a `s.startswith('cde', 2)` (so no function call, no plethora of 
other objects being created and destroyed), and slicing was still twice as fast.

--
~Ethan~


More information about the Python-ideas mailing list