"compare set alternatives performance" import timer, sys import set, fastset def setops(Class): # 3.X: range okay a = Class(range(50)) # a 50-integer set b = Class(range(20)) # a 20-integer set c = Class(range(10)) d = Class(range(5)) for i in range(5): t = a & b & c & d # 3 intersections t = a | b | c | d # 3 unions if __name__ == '__main__': rept = int(sys.argv[1]) print('set => ', timer.test(rept, setops, set.Set)) print('fastset =>', timer.test(rept, setops, fastset.Set))
import stack2 # list-based stacks: [x]+y import stack3 # tuple-tree stacks: (x,y) import stack4 # in-place stacks: y.append(x) import timer # general function timer function rept = 200 from sys import argv pushes, pops, items = eval(argv[1]), eval(argv[2]), eval(argv[3]) def stackops(stackClass): #print stackClass.__module__ x = stackClass('spam') # make a stack object for i in range(pushes): x.push(i) # exercise its methods for i in range(items): t = x[i] for i in range(pops): x.pop() print 'stack2:', timer.test(rept, stackops, stack2.Stack) # pass class to test print 'stack3:', timer.test(rept, stackops, stack3.Stack) # rept*(push+pop+ix) print 'stack4:', timer.test(rept, stackops, stack4.Stack)
"compare set alternatives performance" import timer, sys import set, fastset def setops(Class): # 3.X: rnage okay a = Class(range(50)) # a 50-integer set b = Class(range(20)) # a 20-integer set c = Class(range(10)) d = Class(range(5)) for i in range(5): t = a & b & c & d # 3 intersections t = a | b | c | d # 3 unions if __name__ == '__main__': rept = int(sys.argv[1]) print('set => ', timer.test(rept, setops, set.Set)) print('fastset=> ', timer.test(rept, setops, fastset.Set))
"compare performance of stack alternatives" import stack2 # list-based stacks: [x]+y import stack3 # tuple-tree stacks: (x,y) import stack4 # in-place stacks: y.append(x) import timer # general function timer utility rept = 200 from sys import argv pushes, pops, items = (int(arg) for arg in argv[1:]) def stackops(stackClass): x = stackClass('spam') # make a stack object for i in range(pushes): x.push(i) # exercise its methods for i in range(items): t = x[i] # 3.X: range generator for i in range(pops): x.pop() # or mod = __import__(n) for mod in (stack2, stack3, stack4): # rept*(push+pop+ix) print('%s:' % mod.__name__, end=' ') print(timer.test(rept, stackops, getattr(mod, 'Stack')))
from settime import doit import set, fastset import timer, sys print 'start...' print timer.test(eval(sys.argv[1]), lambda i: doit(set.Set, i)) print timer.test(eval(sys.argv[1]), lambda i: doit(fastset.Set, i))