Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so dont bother with any of their useless mail servers here and just use oauth login instead. Thank the nice Russians for causing that. :)
Paste
Pasted as Python by yobatest2 ( 16 years ago )
import time, random, inspect
def timer(m, f, times=1):
r = range(int(times))
a = len(inspect.getargspec(f).args)
t0 = time.time()
if times == 1: f()
elif a:
for i in r:
f(i)
else:
for i in r:
f()
print("%s%.3f"%(m, time.time() - t0))
a = []
timer("array / list:\n1m inserts: ", lambda i: a.append(i), 1e6)
timer("another 5m inserts: ", lambda i: a.append(i), 5e6)
timer("functional (i**2) 4m-array generation: ", lambda: [i**2 for i in range(4000000)])
def test(i):
a[i] = i
timer("6m assigns: ", test, 6e6)
timer("6m gets: ", lambda i: a[i], 6e6)
a = a[:2000000]
timer("for 2m-array:\nrandom shuffle: ", lambda: random.shuffle(a))
timer("sort by results of (i % 1000): ", lambda: sorted(a, key=lambda i: i % 1000))
timer("find all elements satisfy (i % 1000 == 0): ", lambda: list(filter(lambda i: i % 1000 == 0, a)))
timer("simple sort: ", lambda: a.sort())
h = {}
def test(i):
h[i] = i**2
timer("hash / dict:\n2m assigns: ", test, 2e6)
timer("2m gets: ", lambda i: h[i], 2e6)
def test():
for i in range(-1000000, 1000000):
h.get(i)
timer("1m gets +1m failgets: ", test)
s, ss = "yoba", ""
timer("eval expression 100k times: ", lambda: eval('s + ss'), 1e5)
timer("1m string products: ", lambda: s*100, 1e6)
def test():
s, ss = "yoba", ""
for i in range(1000000):
ss += s
timer("1m concatenations: ", test)
ss = "yoba"*1000000
timer("1m slices (range): ", lambda i: ss[i : i+4], 1e6)
class A(object):
def test(self, i):
self.x = i
a = A()
timer("10m times set instance variable: ", a.test, 1e7)
timer("10m times get instance variable: ", lambda: a.x, 1e7)
def yoba(n=0):
try: return yoba(n+1)
except: return n
print("recursive calls:\nstack len: %d"%yoba())
def test(n=0):
while n < 5000000:
n = yoba(n)
timer("recurse until totally 5m recursions done: ", test)
Revise this Paste