unicode() vs. s.decode()

Michael Fötsch foetsch at yahoo.com
Sat Aug 8 18:02:48 CEST 2009

Michael Ströder wrote:
 > >>> timeit.Timer("unicode('äöüÄÖÜß','utf-8')").timeit(10000000)
 > 17.23644495010376
 > >>> timeit.Timer("'äöüÄÖÜß'.decode('utf8')").timeit(10000000)
 > 72.087096929550171
 > That is significant! So the winner is:
 > unicode('äöüÄÖÜß','utf-8')

Which proves that benchmark results can be misleading sometimes. :-)

unicode() becomes *slower* when you try "UTF-8" in uppercase, or an 
entirely different codec, say "cp1252":

   >>> timeit.Timer("unicode('äöüÄÖÜß','UTF-8')").timeit(1000000)
   >>> timeit.Timer("'äöüÄÖÜß'.decode('UTF-8')").timeit(1000000)
   >>> timeit.Timer("unicode('äöüÄÖÜß','cp1252')").timeit(1000000)
   >>> timeit.Timer("'äöüÄÖÜß'.decode('cp1252')").timeit(1000000)

The reason seems to be that unicode() bypasses codecs.lookup() if the 
encoding is one of "utf-8", "latin-1", "mbcs", or "ascii". OTOH, 
str.decode() always calls codecs.lookup().

If speed is your primary concern, this will give you even better 
performance than unicode():

   decoder = codecs.lookup("utf-8").decode
   for i in xrange(1000000):

However, there's also a functional difference between unicode() and 

unicode() always raises an exception when you try to decode a unicode 
object. str.decode() will first try to encode a unicode object using the 
default encoding (usually "ascii"), which might or might not work.

Kind Regards,

More information about the Python-list mailing list