Last active
May 2, 2021 01:07
-
-
Save holmanb/84be00eab35477565cb95a1d62a741a9 to your computer and use it in GitHub Desktop.
Python String Concatenation Performance Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# concatenate_test.py | |
# based on https://stackoverflow.com/a/24718551/7407726 | |
# modified to include f string benchmarking for a task I'm working on now, might come in handy later | |
from __future__ import print_function | |
import timeit | |
domain = 'some_really_long_example.com' | |
lang = 'en' | |
path = 'some/really/long/path/' | |
iterations = 1000000 | |
_op = "+" | |
cpv_re = "whonamedthis" | |
cp_re = "cp" | |
_slot_separator = "separator" | |
_slot_loose = "blue" | |
_repo = "http://some_really_long_example.com./" | |
_use = "test" | |
def meth_plus(): | |
'''Using + operator''' | |
return '^(?P<without_use>(?:' + \ | |
'(?P<op>' + _op + cpv_re + ')|' + \ | |
'(?P<star>=' + cpv_re + r'\*)|' + \ | |
'(?P<simple>' + cp_re + '))' + \ | |
'(' + _slot_separator + _slot_loose + ')?' + \ | |
_repo + ')(' + _use + ')?$' | |
def meth_join(): | |
'''Using ''.join()''' | |
return ''.join(['^(?P<without_use>(?:' , | |
'(?P<op>' , _op + cpv_re , ')|' , | |
'(?P<star>=' , cpv_re , r'\*)|' , | |
'(?P<simple>' , cp_re , '))' , | |
'(' , _slot_separator , _slot_loose , ')?' , | |
_repo , ')(' , _use , ')?$']) | |
def meth_form(): | |
'''Using string.format''' | |
#return 'http://{0}/{1}/{2}'.format(domain, lang, path) | |
return r'^(?P<without_use>(?:(?P<op>{0}{1})|(?P<star>={2}\*)|(?P<simple>{3}))({4}{5})?{6})({7})?$' \ | |
.format(_op, cpv_re, cpv_re, cp_re, _slot_separator, _slot_loose, _repo, _use) | |
def meth_intp(): | |
'''Using string interpolation''' | |
return r'^(?P<without_use>(?:(?P<op>%s%s)|(?P<star>=%s\*)|(?P<simple>%s))(%s%s)?%s)(%s)?$' \ | |
%(_op, cpv_re, cpv_re, cp_re, _slot_separator, _slot_loose, _repo, _use) | |
#return 'http://%s/%s/%s' % (domain, lang, path) | |
def meth_f(): | |
'''Using string interpolation''' | |
return fr"^(?P<without_use>(?:(?P<op>{_op}{cpv_re})|(?P<star>={cpv_re}\*)|(?P<simple>{cp_re}))({_slot_separator}{_slot_loose})?{_repo})({_use})?$" | |
plus = timeit.Timer(stmt="meth_plus()", setup="from __main__ import meth_plus") | |
join = timeit.Timer(stmt="meth_join()", setup="from __main__ import meth_join") | |
form = timeit.Timer(stmt="meth_form()", setup="from __main__ import meth_form") | |
intp = timeit.Timer(stmt="meth_intp()", setup="from __main__ import meth_intp") | |
f = timeit.Timer(stmt="meth_f()", setup="from __main__ import meth_f") | |
plus.val = plus.timeit(iterations) | |
join.val = join.timeit(iterations) | |
form.val = form.timeit(iterations) | |
intp.val = intp.timeit(iterations) | |
f.val = f.timeit(iterations) | |
min_val = min([plus.val, join.val, form.val, intp.val, f.val]) | |
print(meth_plus()) | |
print(meth_join()) | |
print(meth_form()) | |
print(meth_intp()) | |
print('plus %0.12f (%0.2f%% as fast)' % (plus.val, (100 * min_val / plus.val), )) | |
print('join %0.12f (%0.2f%% as fast)' % (join.val, (100 * min_val / join.val), )) | |
print('form %0.12f (%0.2f%% as fast)' % (form.val, (100 * min_val / form.val), )) | |
print('intp %0.12f (%0.2f%% as fast)' % (intp.val, (100 * min_val / intp.val), )) | |
print('f %0.12f (%0.2f%% as fast)' % (f.val, (100 * min_val / f.val), )) | |
# Sample Output | |
# Python 3.8.8 / Linux Kernel 5.10.27 | |
# plus 4.329498328036 (33.14% as fast) | |
# join 2.099155577016 (68.35% as fast) | |
# form 4.415611634031 (32.49% as fast) | |
# intp 2.479019330000 (57.88% as fast) | |
# f 1.434819712013 (100.00% as fast) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment