[Python-Dev] Performance of u()
Antoine Pitrou
solipsis at pitrou.net
Sun Feb 26 18:53:31 CET 2012
On Sat, 25 Feb 2012 19:13:26 -0800
Guido van Rossum <guido at python.org> wrote:
> If this can encourage more projects to support Python 3 (even if it's
> only 3.3 and later) and hence improve adoption of Python 3, I'm all
> for it.
>
> A small quibble: I'd like to see a benchmark of a 'u' function implemented in C.
Even without implementing it in C, caching the results makes it much
less prohibitive in tight loops:
if sys.version_info >= (3, 0):
def u(value):
return value
else:
def u(value, _lit_cache={}):
if value in _lit_cache:
return _lit_cache[value]
s = _lit_cache[value] = unicode(value, 'unicode-escape')
return s
u'\N{SNOWMAN}barbaz' -> 100000000 loops, best of 3: 0.00928 usec per loop
u('\N{SNOWMAN}barbaz') -> 10000000 loops, best of 3: 0.15 usec per loop
u'foobarbaz_%d' % x -> 1000000 loops, best of 3: 0.424 usec per loop
u('foobarbaz_%d') % x -> 1000000 loops, best of 3: 0.598 usec per loop
Regards
Antoine.
More information about the Python-Dev
mailing list