20.07.13 14:16, Joshua Landau написав(ла):
On 19 July 2013 18:29, Serhiy Storchaka <storch...@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.
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'}).
--
http://mail.python.org/mailman/listinfo/python-list