Szerintem nem csak az optimalizációval van gond, hanem a kódduplikációval is.
Leteszteltem a lehetséges python jittereket az alábbi kóddal:
import time, re
def f(x):
back = []
for i in range(0,x):
if i not in back:
back.append(i)
return back[1:10]
s = time.time()
back = f(20000).index(1)
print("Indexing one", time.time() - s)
s = time.time()
back = f(20000).index(1) + f(20000).index(2)
print("Indexing two", time.time() - s)
pattern = "[A]+" * 12
test_str = "A" * 1000
s = time.time()
[len(re.match(pattern, test_str).group(0)) for i in range(1000)]
print("Matching one", time.time() - s)
s = time.time()
[len(re.match(pattern, test_str).group(0)) for i in range(1000) if re.match(pattern, test_str)]
print("Matching two", time.time() - s)
CPython2
('Indexing one', 1.9690001010894775)
('Indexing two', 3.921999931335449)
('Matching one', 0.05299997329711914)
('Matching two', 0.0969998836517334)
Pypy2 6.0.0
('Indexing one', 0.05799984931945801)
('Indexing two', 0.11500000953674316)
('Matching one', 0.07500004768371582)
('Matching two', 0.10299992561340332)
IronPython + .net4.5
('Indexing one', 4.1027679443359375)
('Indexing two', 8.1740570068359375)
('Matching one', 0.07904052734375)
('Matching two', 0.14720916748046875)
IronPython + .netcore2.0
('Indexing one', 3.7830429077148438)
('Indexing two', 7.5531768798828125)
('Matching one', 0.09737396240234375)
('Matching two', 0.16550445556640625)
Python 3.6 + numba
Indexing one 0.2415463924407959
Indexing two 0.16579389572143555
Matching one 0.07383203506469727
Matching two 0.08798456192016602
Szóval a numba-n kívül a többi nem végzett ilyen jellegű optimalizációt. (.net esetében lehet, hogy az ironpython implementáció miatt)
Érdekességképpen megnéztem javascripttel is:
Chrome
Indexing one: 221.033935546875ms
Indexing two: 437.77392578125ms
Firefox
Indexing one: 184ms
Indexing two: 352ms
Edge
Indexing one: 103,225ms
Indexing two: 202,515ms