1 2 3 4 5 6 7 8 9 10 11 12 13 | from time import time t = time() list = ['a','b','is','python','jason','hello','hill','with','phone','test', 'dfdf','apple','pddf','ind','basic','none','baecr','var','bana','dd','wrd'] #list = dict.fromkeys(list,True) print list filter = [] for i in range (1000000): for find in ['is','hat','new','list','old','.']: if find not in list: filter.append(find) print "total run time:" print time()-t |
1 2 3 4 5 6 7 8 9 10 11 12 13 | from time import time t = time() lista=[1,2,3,4,5,6,7,8,9,13,34,53,42,44] listb=[2,4,6,9,23] intersection=[] for i in range (1000000): for a in lista: for b in listb: if a == b: intersection.append(a) print "total run time:" print time()-t |
1 2 | total run time: 38.4070000648 |
1 2 3 4 5 6 7 8 9 | from time import time t = time() lista=[1,2,3,4,5,6,7,8,9,13,34,53,42,44] listb=[2,4,6,9,23] intersection=[] for i in range (1000000): list(set(lista)&set(listb)) print "total run time:" print time()-t |
语法 | 操作 | 说明 |
set(list1) | set(list2) | union | 包含 list1 和 list2 所有数据的新集合 |
set(list1) & set(list2) | intersection | 包含 list1 和 list2 中共同元素的新集合 |
set(list1) – set(list2) | difference | 在 list1 中出现但不在 list2 中出现的元素的集合 |
1 2 3 4 5 6 7 8 9 10 | from time import time t = time() lista = [1,2,3,4,5,6,7,8,9,10] listb =[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.01] for i in range (1000000): for a in range(len(lista)): for b in range(len(listb)): x=lista[a]+listb print "total run time:" print time()-t |
1 2 3 4 5 6 7 8 9 10 11 12 13 | from time import time t = time() lista = [1,2,3,4,5,6,7,8,9,10] listb =[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.01] len1=len(lista) len2=len(listb) for i in xrange (1000000): for a in xrange(len1): temp=lista[a] for b in xrange(len2): x=temp+listb print "total run time:" print time()-t |
1 2 3 4 5 6 7 8 9 10 | from time import time t = time() abbreviations = ['cf.', 'e.g.', 'ex.', 'etc.', 'fig.', 'i.e.', 'Mr.', 'vs.'] for i in range (1000000): for w in ('Mr.', 'Hat', 'is', 'chasing', 'the', 'black', 'cat', '.'): if w in abbreviations: #if w[-1] == '.' and w in abbreviations: pass print "total run time:" print time()-t |
2 3 4 5 6 7 8 9 10 | from time import time t = time() s = "" list = ['a','b','b','d','e','f','g','h','i','j','k','l','m','n'] for i in range (10000): for substr in list: s+= substr print "total run time:" print time()-t |
1 2 3 | s = "" for x in list: s += func(x) |
1 2 | slist = [func(elt) for elt in somelist] s = "".join(slist) |
1 | out = "<html>%s%s%s%s</html>" % (head, prologue, query, tail) |
1 | out = "<html>" + head + prologue + query + tail + "</html>" |
1 2 3 4 5 6 7 8 9 10 | from time import time t = time() list = ['a','b','is','python','jason','hello','hill','with','phone','test', 'dfdf','apple','pddf','ind','basic','none','baecr','var','bana','dd','wrd'] total=[] for i in range (1000000): for w in list: total.append(w) print "total run time:" print time()-t |
1 2 | for i in range (1000000): a = [w for w in list] |
1 2 3 4 5 6 | >>> from timeit import Timer >>> Timer("t=a;a=b;b=t","a=1;b=2").timeit() 0.25154118749729365 >>> Timer("a,b=b,a","a=1;b=2").timeit() 0.17156677734181258 >>> |
1 2 3 4 5 6 7 8 9 | import profile def profileTest(): Total =1; for i in range(10): Total=Total*(i+1) print Total return Total if __name__ == "__main__": profile.run("profileTest()") |
1 2 3 | import pstats p = pstats.Stats('testprof') p.sort_stats("name").print_stats() |
1 2 3 4 5 6 7 | C:\Documents and Settings\Administrator>pypy Python 2.7.2 (0e28b379d8b3, Feb 09 2012, 18:31:47) [PyPy 1.8.0 with MSC v.1500 32 bit] on win32 Type "help", "copyright", "credits" or "license" for more information. And now for something completely different: ``PyPy is vast, and contains multitudes'' >>>> |
2 3 4 5 6 7 | C:\Documents and Settings\Administrator\ 桌面 \doc\python>pypy loop.py total run time: 8.42199993134 C:\Documents and Settings\Administrator\ 桌面 \doc\python>python loop.py total run time: 106.391000032 |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |