comparison mercurial/pycompat.py @ 31424:4acc49335a6e

py3: optimize py3 compat.bytechr using Struct.pack With Python 3.4.3, timeit says 0.437 usec -> 0.0685 usec. With Python 3.6, timeit says 0.157 usec -> 0.0907 usec. So it's faster on both versions, but the speedup varies a lot. Thanks to Gregory Szorc for the suggestion.
author Martin von Zweigbergk <martinvonz@google.com>
date Wed, 15 Mar 2017 09:30:50 -0700
parents fb1f70331ee6
children 63a39d647888
comparison
equal deleted inserted replaced
31423:568d80b24b3a 31424:4acc49335a6e
36 36
37 if ispy3: 37 if ispy3:
38 import builtins 38 import builtins
39 import functools 39 import functools
40 import io 40 import io
41 import struct
41 42
42 fsencode = os.fsencode 43 fsencode = os.fsencode
43 fsdecode = os.fsdecode 44 fsdecode = os.fsdecode
44 # A bytes version of os.name. 45 # A bytes version of os.name.
45 osname = os.name.encode('ascii') 46 osname = os.name.encode('ascii')
71 # TODO: On Windows, the native argv is wchar_t, so we'll need a different 72 # TODO: On Windows, the native argv is wchar_t, so we'll need a different
72 # workaround to simulate the Python 2 (i.e. ANSI Win32 API) behavior. 73 # workaround to simulate the Python 2 (i.e. ANSI Win32 API) behavior.
73 if getattr(sys, 'argv', None) is not None: 74 if getattr(sys, 'argv', None) is not None:
74 sysargv = list(map(os.fsencode, sys.argv)) 75 sysargv = list(map(os.fsencode, sys.argv))
75 76
76 def bytechr(i): 77 bytechr = struct.Struct('>B').pack
77 return bytes([i])
78 78
79 def iterbytestr(s): 79 def iterbytestr(s):
80 """Iterate bytes as if it were a str object of Python 2""" 80 """Iterate bytes as if it were a str object of Python 2"""
81 return iter(s[i:i + 1] for i in range(len(s))) 81 return iter(s[i:i + 1] for i in range(len(s)))
82 82