[Python-ideas] string codes & substring equality

Steven D'Aprano steve at pearwood.info
Mon Dec 2 01:02:00 CET 2013


On Sun, Dec 01, 2013 at 12:55:58PM -0800, Ethan Furman wrote:

> >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'm reminded of a joke. What's 9/2?

The mathematician says it's 4.5. The scientist says it's 4.5 ± 0.1. The 
engineer says it's between 4 and 5, but we'll call it 6 to be on the 
safe side.

Apparently I'm an engineer at heart :-)

> 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.

Did you try it with huge strings? Say, a million Unicode characters?


-- 
Steven


More information about the Python-ideas mailing list