import cStringIO import os import operator import sys getTime = os.times def deltaTime(start, end): return int((sum(end[:2]) - sum(start[:2])) * 1000) if len(sys.argv) < 2: print "usage: " + sys.argv[0] + " testnumber" print "testnumber should be a number from 1 to 22 (inclusive)" sys.exit(0) test = int(sys.argv[1]) print "test #" + str(test) def addTwoThings(a, b): return a + b def addThreeThings(a, b, c): return a + b + c def addFourThings(a, b, c, d): return a + b + c + d def addFiveThings(a, b, c, d, e): return a + b + c + d + e def addTwentyThings(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t): return a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r + s + t start = getTime() if test == 1: for i in xrange(10000000): x = addTwentyThings("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo", "ppp", "qqq", "rrr", "sss", "ttt") if test == 2: for i in xrange(10000000): x = "aaa" + "bbb" + "ccc" + "ddd" + "eee" + "fff" + "ggg" + "hhh" + "iii" + "jjj" + "kkk" + "lll" + "mmm" + "nnn" + "ooo" + "ppp" + "qqq" + "rrr" + "sss" + "ttt" if test == 3: for i in xrange(10000000): x = "".join(["aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo", "ppp", "qqq", "rrr", "sss", "ttt"]) if test == 4: for i in xrange(10000000): x = "aaa" x += "bbb" x += "ccc" x += "ddd" x += "eee" x += "fff" x += "ggg" x += "hhh" x += "iii" x += "jjj" x += "kkk" x += "lll" x += "mmm" x += "nnn" x += "ooo" x += "ppp" x += "qqq" x += "rrr" x += "sss" x += "ttt" if test == 5: for i in xrange(10000000): array = [] array.append("aaa") array.append("bbb") array.append("ccc") array.append("ddd") array.append("eee") array.append("fff") array.append("ggg") array.append("hhh") array.append("iii") array.append("jjj") array.append("kkk") array.append("lll") array.append("mmm") array.append("nnn") array.append("ooo") array.append("ppp") array.append("qqq") array.append("rrr") array.append("sss") array.append("ttt") x = "".join(array) # not even in the running -- *way* slower if test == 6: for i in xrange(1000000): x = cStringIO.StringIO() x.write("aaa") x.write("bbb") x.write("ccc") x.write("ddd") x.write("eee") x.write("fff") x.write("ggg") x.write("hhh") x.write("iii") x.write("jjj") x.write("kkk") x.write("lll") x.write("mmm") x.write("nnn") x.write("ooo") x.write("ppp") x.write("qqq") x.write("rrr") x.write("sss") x.write("ttt") if test == 7: for i in xrange(10000000): x = "a" + "b" if test == 8: for i in xrange(10000000): x = "a" + "b" + "c" if test == 9: x = "" for i in xrange(10000000): x += "a" if test == 10: for i in xrange(10000000): x = addTwoThings("aaa", "bbb") y = x[1] if test == 11: for i in xrange(10000000): x = addThreeThings("aaa", "bbb", "ccc") y = x[1] if test == 12: x = "" for i in xrange(10000000): x += "a" y = x[1] if test == 13: x = [] for i in xrange(10000000): x.append("a") y = "".join(x) if test == 14: x = "" for i in xrange(10000): x = "a" + x y = x[1] if test == 15: x = "" # be warned: this hits the pathological case in normal python. # it took almost 16 minutes on my Athlon 64 x2 4400+! # (run with concat, it only took about a half-second .) for i in xrange(1000000): x = "a" + x y = x[1] if test == 16: x = "" # don't run this with non-concat python! # this is *way* more than 10x worse than 1m. # I got bored after 25 minutes. for i in xrange(10000000): x = "a" + x y = x[1] if test == 17: x = cStringIO.StringIO() for i in xrange(1000000): x.write("axa") if test == 18: x = [] xappend = x.append for i in xrange(10000000): xappend("a") y = "".join(x) if test == 19: x = "".__add__ for i in xrange(10000000): x = x("a").__add__ x = x("") print len(x) y = x[1] if test == 20: x = [] for i in xrange(10000000): x.insert(0, "a") y = "".join(x) if test == 21: for i in xrange(10000000): x = addFourThings("aaa", "bbb", "ccc", "ddd") y = x[1] if test == 22: for i in xrange(10000000): x = addFiveThings("aaa", "bbb", "ccc", "ddd", "eee") y = x[1] end = getTime() print "elapsed: " + str(deltaTime(start, end)) + "ms"