JGoutin added the comment: A little encoding cache benchmark.
Current Code: ============= import sys def _fscodec(): encoding = sys.getfilesystemencoding() errors = sys.getfilesystemencodeerrors() def fsencode(filename): filename = fspath(filename) # Does type-checking of `filename`. if isinstance(filename, str): return filename.encode(encoding, errors) else: return filename def fsdecode(filename): filename = fspath(filename) # Does type-checking of `filename`. if isinstance(filename, bytes): return filename.decode(encoding, errors) else: return filename return fsencode, fsdecode fsencode, fsdecode = _fscodec() del _fscodec --------- import os %timeit os.fsdecode(b'\xc3\xa9') The slowest run took 21.59 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 449 ns per loop %timeit os.fsencode('é') The slowest run took 24.20 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 412 ns per loop Modified Code: ============== from sys import getfilesystemencoding, getfilesystemencodeerrors def fsencode(filename): filename = fspath(filename) # Does type-checking of `filename`. if isinstance(filename, str): return filename.encode(getfilesystemencoding(), getfilesystemencodeerrors()) else: return filename def fsdecode(filename): filename = fspath(filename) # Does type-checking of `filename`. if isinstance(filename, bytes): return filename.decode(getfilesystemencoding(), getfilesystemencodeerrors()) else: return filename --------- import os %timeit os.fsdecode(b'\xc3\xa9') The slowest run took 15.88 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 541 ns per loop %timeit os.fsencode('é') The slowest run took 19.32 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 502 ns per loop Result: ======= Cache is a 17% speed up optimization. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue29241> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com