Find and Replace Simplification

Joshua Landau joshua at landau.ws
Sat Jul 20 13:03:58 EDT 2013


On 20 July 2013 12:57, Serhiy Storchaka <storchaka at gmail.com> wrote:
> 20.07.13 14:16, Joshua Landau написав(ла):
>>
>> On 19 July 2013 18:29, Serhiy Storchaka <storchaka at gmail.com> wrote:
>>>
>>> The string replace() method is fastest (at least in Python 3.3+). See
>>> implementation of html.escape() etc.
>>
>>
>> def escape(s, quote=True):
>>      if quote:
>>          return s.translate(_escape_map_full)
>>      return s.translate(_escape_map)
>>
>> I fail to see how this supports the assertion that str.replace() is
>> faster.
>
>
> And now look at Python 3.4 sources.

I'll just trust you ;).

>> However, some quick timing shows that translate has a very
>> high penalty for missing characters and is a tad slower any way.
>>
>> Really, though, there should be no reason for .translate() to be
>> slower than replace -- at worst it should just be "reduce(lambda s,
>> ab: s.replace(*ab), mapping.items()¹, original_str)" and end up the
>> *same* speed as iterated replace.
>
>
> It doesn't work such way. Consider
> 'ab'.translate({ord('a'):'b',ord('b'):'a'}).

*sad*

Still, it seems to me that it should be optimizable for sensible
builtin types such that .translate is significantly faster, as there's
no theoretical extra work that .translate *has* to do that .replace
does not, and .replace also has to rebuild the string a lot of times.



More information about the Python-list mailing list