Message313071
The mapping passed to str.translate must map ints representing codepoints to either either ints or strings. Translate can extract binary codepoints for the new string from either. Ints are slightly faster, so I am inclined not to switch.
import timeit
class ParseMapN(dict):
def __missing__(self, key): return 120
class ParseMapS(dict):
def __missing__(self, key): return 'x'
trans1 = ParseMapN.fromkeys(range(128), 120)
trans1.update((ord(c), ord('(')) for c in "({[")
trans1.update((ord(c), ord(')')) for c in ")}]")
trans1.update((ord(c), ord(c)) for c in "\"'\\\n#")
trans2 = ParseMapN.fromkeys(range(128), 'x')
trans2.update((ord(c), '(') for c in "({[")
trans2.update((ord(c), ')') for c in ")}]")
trans2.update((ord(c), c) for c in "\"'\\\n#")
for i in (1, 10, 100, 1000):
code = '\t a([{b}])b"c\'d\n' * i
print('N ', i)
print(timeit.repeat(
'code.translate(trans1)',
number=10000, globals = globals()))
print('S ', i)
print(timeit.repeat(
'code.translate(trans2)',
number=10000, globals = globals()))
N 1 [0.056562504, 0.056747570, 0.05654651, 0.056460749, 0.056428776]
S 1 [0.060795346, 0.062304155, 0.061043432, 0.062349345, 0.061191301]
N 10 [0.076474600, 0.076463227, 0.076560984, 0.076581582, 0.076010091]
S 10 [0.080739106, 0.080798745, 0.08034192, 0.080987501, 0.080617369]
N 100 [0.28529922, 0.28383868, 0.283949046, 0.284461512, 0.284291203]
S 100 [0.289629521, 0.288535418, 0.289154560, 0.28811548, 0.28862180]
N1000 [2.23882157, 2.2383192, 2.2384120, 2.2377972, 2.23854982]
S1000 [2.24242237, 2.2426845, 2.2424623, 2.2420030, 2.24254871]
The pattern of all S repeats being greater than all corresponding N repeats was true for 2 other runs. |
|
Date |
User |
Action |
Args |
2018-02-28 21:49:18 | terry.reedy | set | recipients:
+ terry.reedy, serhiy.storchaka, cheryl.sabella |
2018-02-28 21:49:18 | terry.reedy | set | messageid: <1519854558.25.0.467229070634.issue32940@psf.upfronthosting.co.za> |
2018-02-28 21:49:18 | terry.reedy | link | issue32940 messages |
2018-02-28 21:49:17 | terry.reedy | create | |
|