Прикреплённый файл «compare.py»
Загрузка 1 #!/usr/bin/python
2 # coding: utf8
3 '''
4 Сравнить производительность dict, list и array по заполнению, чтению и записи, нарисовать красивые картинки в PyGame
5 '''
6 import sys, random, os, array, pygame
7
8 N=len(sys.argv)>1 and int(sys.argv[1]) or 5000000
9 W=50
10
11 S=random.sample(xrange(N), N)
12 D=[random.randrange(N-i) for i in xrange(N/10000)]
13 print "========="
14 def Test(n, constr, write, read, delete):
15 Tc=os.times()[0]
16 s=constr(n)
17 Tc=os.times()[0]-Tc
18 print "."
19 Tw=os.times()[0]
20 for i in S:
21 write(s,i,i)
22 Tw=os.times()[0]-Tw
23 print "."
24 Tr=os.times()[0]
25 for i in S:
26 c=read(s,i)
27 Tr=os.times()[0]-Tr
28 print "."
29 Td=os.times()[0]
30 for i in D:
31 delete(s,i)
32 Td=os.times()[0]-Td
33 print "#"
34 return Tc, Tw, Tr, Td
35
36 def cList(n): return [0]*n
37 def wList(o,i,v): o[i]=v
38 def rList(o,i): return o[i]
39 def dList(o,i): o.pop(i)
40 def cDict(n): return {}
41 def cArray(n): return array.array('L',[0]*n)
42 Tlist=Test(N, cList, wList, rList, dList)
43 Tdict=Test(N, cDict, wList, rList, dList)
44 Tarray=Test(N, cArray, wList, rList, dList)
45 print Tlist
46 print Tdict
47 print Tarray
48 pygame.init()
49 screen=pygame.display.set_mode((1024,768))
50 cols = pygame.Color("red"), pygame.Color("cyan"), pygame.Color("yellow")
51 n=len(Tlist)
52 dx,dy=5,5
53 X=dx
54 R=screen.get_rect()
55 w=(R.width-2*dx)/(3*n)
56 M=max(Tlist+Tdict+Tarray)
57 scale=(R.height-2*dy)/M
58 for tr in zip(Tlist,Tdict,Tarray):
59 for c,v in zip(cols, tr):
60 screen.fill(c,((X,dy),(w-dx*2,int((R.height-2*dy)*v/M))))
61 X+=w-dx
62 X+=dx
63 while pygame.event.wait().type not in (pygame.QUIT, pygame.KEYDOWN):
64 pygame.display.flip()
Прикреплённые файлы
Для ссылки на прикреплённый файл в тексте страницы напишите attachment:имяфайла, как показано ниже в списке файлов. Не используйте URL из ссылки «[получить]», так как он чисто внутренний и может измениться.- [получить | показать] (2011-09-26 11:35:36, 5.7 KB) [[attachment:Tree.py]]
- [получить | показать] (2011-09-26 11:35:36, 1.2 KB) [[attachment:Treecount.py]]
- [получить | показать] (2011-09-26 11:35:36, 2.4 KB) [[attachment:Treeops.py]]
- [получить | показать] (2011-09-26 11:35:36, 1.7 KB) [[attachment:compare.py]]
Вам нельзя прикреплять файлы к этой странице.