Message285888
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. |
|
Date |
User |
Action |
Args |
2017-01-20 07:14:19 | JGoutin | set | recipients:
+ JGoutin, lemburg, paul.moore, vstinner, tim.golden, zach.ware, steve.dower |
2017-01-20 07:14:19 | JGoutin | set | messageid: <1484896459.48.0.320114938992.issue29241@psf.upfronthosting.co.za> |
2017-01-20 07:14:19 | JGoutin | link | issue29241 messages |
2017-01-20 07:14:18 | JGoutin | create | |
|