On Sat, 25 Feb 2012 19:13:26 -0800
Guido van Rossum <[email protected]> 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.
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com