Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Python by yobatest ( 16 years ago )
import time, random
def timer(m, times=1, y=1):
    r = range(int(times))
    t0 = time.time()
    if times == 1: m()
    elif y:
        for i in r:
            m(i)
    else:
        for i in r:
            m()
    print(time.time() - t0)

a = []
print("array / list:\n1m inserts: ", end="")
timer(lambda i: a.append(i**2/3), 1e6)
print("another 5m inserts: ", end="")
timer(lambda i: a.append(i**2), 5e6)
print("create 5m-array with generator: ", end="")
timer(lambda: [i**2 for i in range(5000000)])
print("6m assigns: ", end="")
def test(i):
    a[i] = i
timer(test, 6e6)
print("6m gets: ", end="")
timer(lambda i: a[i], 6e6)
print("random shuffle (6m elements): ", end="")
timer(lambda: random.shuffle(a))
print("quicksort by results of lambda i -> i % 1000: ", end="")
timer(lambda: sorted(a, key=lambda i: i % 1000))
print("find all elements that satisfy lambda i -> i % 1000 == 0: ", end="")
timer(lambda: list(filter(lambda i: i % 1000 == 0, a)))
print("quicksort simple: ", end="")
timer(lambda: a.sort())


h = {}
print("hash / dict:\n1m assigns: ", end="")
def test(i):
    h[i] = i**2
timer(test, 1e6)
print("1m gets: ", end="")
timer(lambda i: h[i], 1e6)
print("1m gets (+1m failgets): ", end="")
def test():
    for i in range(-1000000, 1000000):
        hi = h.get(i)
timer(test)

print("eval expression 100k times: ", end="")
s, ss = "yoba", ""
timer(lambda: eval('s'), 1e5, 0)

print("1m string products: ", end="")
timer(lambda: s*100, 1e6, 0)
print("1m concatenations: ", end="")
def test():
    s, ss = "yoba", ""
    for i in range(1000000):
        ss += s
timer(test)
print("1m slices (range): ", end="")
ss = "yoba"*1000000
timer(lambda i: ss[i : i+4], 1e6)

class A(object):
    def test(self, i):
        self.x = i
a = A()
print("10m times set instance variable: ", end="")
timer(a.test, 1e7)
print("10m times get instance variable: ", end="")
timer(lambda: a.x, 1e7, 0)

 

Revise this Paste

Your Name: Code Language: