4 ms·
few months ago i had to optimize some old python code, .. and found that my longtime assumptions from py 1,2 and early 3.x are not anymore true. So put up some
by svilen_dobrev 2y ago
few months ago i had to optimize some old python code, .. and found that my longtime assumptions from py 1,2 and early 3.x are not anymore true. So put up some comparisons.. Check them here:
https://github.com/svilendobrev/transit-python3/blob/master/benchmark/timing-probi.py https://github.com/svilendobrev/transit-python3/blob/master/...
(comment out the import transit.* and the two checks after it as they are specific. Takes ~25 seconds to finish)
Results like below. Most make sense, after thinking deeper about it, but some are weird.
One thing stays axiomatic though: no way of doing something is faster than not doing it at all.
Lesson: measure before assuming anything "from-not-so-fresh-experience".
btw, unrelated, probably will be looking for work next month.
Have fun.
$ python timing-probi.py
...
:::: c_pfx_check ::::
f_tuple 0.10805996900126047
f_list 0.10568888399939169
f_tuple_global 0.10741564899944933
f_list_global 0.10980218799886643
f_dict_global 0.09630626599937386
f_tuple_global20 0.6103107449998788
f_list_global20 0.6878404369999771
f_one_by_one 0.05088467200039304
:::: c_func_glob_vs_staticmethod ::::
f_glob_func 0.08005491699987033
f_staticmethd 0.10022392999962904
:::: c_for_loop_vs_gen_for ::::
for_loop 0.2255296620005538
gen_loop 0.29973782500019297
:::: c_dictget_vs_dict_subscr ::::
dictgetattr_get 0.05093873599980725
dictfuncget 0.048424991000501905
dictin_dictsubscr 0.04722780499832879
dictsubscr 0.04069488099958107
:::: c_listget_vs_list_subscr ::::
listgetattr_get 0.0779018819994235
listfuncget 0.07271830799982126
listsubscr 0.057812218999970355
:::: c_property_vs_funccall ::::
property 0.08194994600125938
funccall 0.08422214100028214
:::: c_a_in_abc_vs_a_eq_b_or ::::
x_in_str 0.04265176899934886
x_in_tuple 0.0530087259994616
x_in_tuple_global 0.05479079300130252
x_eq_a_or 0.049807468998551485
:::: c_tuple_vs_slots ::::
slots 0.17708088200015482
plain 0.18551878399921407
tuple 0.07675717399979476
dict 0.14878148099887767
namedtuple 0.2637523979992693
dataclass 0.18731526199917425
dataclassfrozen 0.38634534500124573
:::: c_dictcomp_vs_dict_gen_tuples_vs_loop ::::
dictcomp 4.476536423999278
dict_gen_tuples 7.045798945999195
dict_listcomp 6.461099333000675
dictloop 4.889642943000581
:::: c_listcomp_vs_list_gen_vs_loop ::::
listcomp 1.4269603859993367
list_gen 2.6340354429994477
listloop1 1.856299590001072
listloop2 2.1041324060006446
:::: c_funccall_args_vs_kargs ::::
args 0.14374798999961058
kargs 0.1689707850000559
kargs_ignored 0.2658924890001799
kargs_default 0.26562809300048684