3. Introducton
def qks(ls):
if not ls : return ls
p, ls = ls[0], ls[1:]
return qks([i for i in ls if i < p]) + [p]
+ qks([i for i in ls if i >= p])
if __name__ == "__main__":
import random
ls = [random.randint(0, 100) for i in xrange(10)]
4. Agenda
time utility, time module, timeit, sys.getsizeof
Module: profile, cProfile, hotshot, pystats
Line Profiler, Memory Profiler, Objgraph
RunSnakeRun
In Action
Conclusion
13. timeit
In [1]: from timeit import timeit
In [2]: timeit('s.appendleft(3)',
...: 'import collections; s= collections.deque()', number=1000000)
Out[2]: 0.1150519847869873
In [3]: timeit('s.insert(0, 3)', 's=[]', number=1000000)
Out[3]: 392.4638919830322
14. timeit
不同平台使用的time不同(Xinux 使用 `time.time()`, Windows 使用
`time.clock()`)
python -m timeit -s "import collections; s = collections.deque()" -n
1000000 "s.appendleft(3)"
15. sys.getsizeof
import sys
def sys_getsizeof():
a = range(1000000)
b = xrange(1000000)
c = [i for i in xrange(1000000)]
print sys.getsizeof(a)
print sys.getsizeof(b)
print sys.getsizeof(c)
16. sys.getsizeof
import sys
def sys_getsizeof():
a = range(1000000)
b = xrange(1000000)
c = [i for i in xrange(1000000)]
print sys.getsizeof(a) #8000072
print sys.getsizeof(b) #40
print sys.getsizeof(c) #8697472
17. sys.getsizeof
class Entity(object):
def __init__(self, name, age):
self.name = name
self.age = age
es = [Entity("dreampuf", 100) for i in xrange(1000000)]
print sys.getsizeof(es) #8697472
print sum([sys.getsizeof(i) for i in es]) #64000000
25. profile vs. cProfile vs. hotshot
实现 开销 维护 平台
Python
profile Python 高 持续
Like
Python C
cProfile 中 持续 CPython
API
hotshot C module 低 停止 CPython
26. Line Profiler
from line_profiler import LineProfiler
def line_profiler():
p = LineProfiler()
dirs_lineprofiler = p(dirs)
dirs_lineprofiler(".")
p.print_stats()
27. Line Profiler
dreampuf@HX:~/hub/fortest$ python test_timeit.py
Timer unit: 1e-06 s
File: test_timeit.py
Function: dirs at line 101
Total time: 0.108716 s
Line # Hits Time Per Hit % Time Line Contents
==============================================================
101 def dirs(dirpath):
102 1 1129 1129.0 1.0 for current, ds, fs in os.walk(dirpath):
103 49 56 1.1 0.1 for f in fs:
104 48 50 1.0 0.0 if f[-3:] not in ('tml', '.py'):
105 26 16 0.6 0.0 continue
106 22 107461 4884.6 98.8 main(os.path.join(current, f))
107 1 4 4.0 0.0 break