Take best-of-ten in most benchmarks, add inner_loop benchmarks

This commit is contained in:
K. Lange 2022-06-02 08:01:55 +09:00
parent 1ee63c15f6
commit 3bc8d72895
8 changed files with 110 additions and 6 deletions

View File

@ -132,5 +132,5 @@ if __name__=='__main__':
read_classvar, read_instancevar, read_unboundmethod, read_boundmethod, call_boundmethod, read_classvar, read_instancevar, read_unboundmethod, read_boundmethod, call_boundmethod,
write_local, write_nonlocal, write_global, write_local, write_nonlocal, write_global,
write_classvar, write_instancevar]: write_classvar, write_instancevar]:
print(timeit(f,number=1000000), f.__qualname__) print(min(timeit(f,number=100000) for x in range(10)), f.__qualname__)

View File

@ -136,5 +136,5 @@ if __name__=='__main__':
read_classvar, read_instancevar, read_unboundmethod, read_boundmethod, call_boundmethod, read_classvar, read_instancevar, read_unboundmethod, read_boundmethod, call_boundmethod,
write_local, write_nonlocal, write_global, write_local, write_nonlocal, write_global,
write_classvar, write_instancevar]: write_classvar, write_instancevar]:
print(timeit(f,number=1000000), f.__qualname__ if hasattr(f,'__qualname__') else f.__name__ if hasattr(f,'__name__') else '?') print(min(timeit(f,number=100000) for x in range(10)), f.__qualname__ if hasattr(f,'__qualname__') else f.__name__ if hasattr(f,'__name__') else '?')

56
bench/inner_loop.krk Normal file
View File

@ -0,0 +1,56 @@
import timeit
if True:
let xs = [[[x for x in range(30)] for y in range(30)] for z in range(30)]
xs[29][29][29] = 42
def breaks():
let _z
for ys in xs:
for zs in ys:
for z in zs:
if z == 42:
_z = z
break
else:
continue
break
else:
continue
break
else:
return None
return _z
class FoundIt(Exception):
pass
def exceptions():
let _z
try:
for ys in xs:
for zs in ys:
for z in zs:
if z == 42:
_z = z
raise FoundIt()
except FoundIt as e:
return _z
def _inner():
for ys in xs:
for zs in ys:
for z in zs:
if z == 42:
return z
def functions():
return _inner()
import gc
gc.collect()
print(min(timeit.timeit(breaks,number=100) for x in range(10)),"breaks")
gc.collect()
print(min(timeit.timeit(functions,number=100) for x in range(10)),"functions")
gc.collect()
print(min(timeit.timeit(exceptions,number=100) for x in range(10)),"exceptions")

48
bench/inner_loop.py Normal file
View File

@ -0,0 +1,48 @@
import fasttimer as timeit
xs = [[[x for x in range(30)] for y in range(30)] for z in range(30)]
xs[29][29][29] = 42
def breaks():
for ys in xs:
for zs in ys:
for z in zs:
if z == 42:
break
else:
continue
break
else:
continue
break
else:
return None
return z
class FoundIt(Exception):
def __init__(self, needle):
self.needle = needle
def exceptions():
try:
for ys in xs:
for zs in ys:
for z in zs:
if z == 42:
raise FoundIt(z)
except FoundIt as e:
return e.needle
def _inner():
for ys in xs:
for zs in ys:
for z in zs:
if z == 42:
return z
def functions():
return _inner()
print(min(timeit.timeit(breaks,number=100) for x in range(10)),"breaks")
print(min(timeit.timeit(functions,number=100) for x in range(10)),"functions")
print(min(timeit.timeit(exceptions,number=100) for x in range(10)),"exceptions")

View File

@ -8,4 +8,4 @@ def func():
add(1) add(1)
pop() pop()
print(timeit(func), "list append") print(min(timeit(func,number=100000) for x in range(10)), "list append")

View File

@ -8,4 +8,4 @@ def func():
add(1) add(1)
pop() pop()
print(timeit(func), "list append") print(min(timeit(func,number=100000) for x in range(10)), "list append")

View File

@ -11,4 +11,4 @@ def makeTree(depth):
if __name__ == '__main__': if __name__ == '__main__':
from timeit import timeit from timeit import timeit
print(timeit(lambda: makeTree(16), number=10), 'makeTree') print(min(timeit(lambda: makeTree(16), number=1) for x in range(10)), 'makeTree')

View File

@ -11,4 +11,4 @@ def makeTree(depth):
if __name__ == '__main__': if __name__ == '__main__':
from fasttimer import timeit from fasttimer import timeit
print(timeit(lambda: makeTree(16), number=10), 'makeTree') print(min(timeit(lambda: makeTree(16), number=1) for x in range(10)), 'makeTree')