python -m pdb myscript.py #注意这会重启myscript.py
可以在程序中这么设置断点:
import pdb; pdb.set_trace()
可以修改变量的值,但是要注意,前面加上!比如要修改final的值,应该这样!final="newvalue"
支持的命令:
p 打印变量
n next
step 细点运行
c continue
l list
a args 打印当前函数的参数
condition bpnumber [condition]
clear/disable/enable 清除/禁用/使能断点
q quit
ncalls
|
函数的被调用次数
|
tottime
|
函数总计运行时间,除去函数中调用的函数运行时间
|
percall
|
函数运行一次的平均时间,等于
tottime/ncalls
|
cumtime
|
函数总计运行时间,含调用的函数运行时间
|
percall
|
函数运行一次的平均时间,等于
cumtime/ncalls
|
filename:lineno(function)
|
函数所在的文件名,函数的行号,函数名
|
# …
略
if __name__ == "__main__":
import profile
profile.run("foo()", "prof.txt")
import pstats
p = pstats.Stats("prof.txt")
p.sort_stats("time").print_stats()
|
strip_dirs()
|
用以除去文件名前名的路径信息。
|
add(filename,[…])
|
把
profile
的输出文件加入
Stats
实例中统计
|
dump_stats(filename)
|
把
Stats
的统计结果保存到文件
|
sort_stats(key,[…])
|
最重要的一个函数,用以排序
profile
的输出
|
reverse_order()
|
把
Stats
实例里的数据反序重排
|
print_stats([restriction,…])
|
把
Stats
报表输出到
stdout
|
print_callers([restriction,…])
|
输出调用了指定的函数的函数的相关信息
|
print_callees([restriction,…])
|
输出指定的函数调用过的函数的相关信息
|
‘ncalls’
|
被调用次数
|
‘cumulative’
|
函数运行的总时间
|
‘file’
|
文件名
|
‘module’
|
文件名
|
‘pcalls’
|
简单调用统计(兼容旧版,未统计递归调用)
|
‘line’
|
行号
|
‘name’
|
函数名
|
‘nfl’
|
Name/file/line
|
‘stdname’
|
标准函数名
|
‘time’
|
函数内部运行时间(不计调用子函数的时间)
|
python -m cProfile -s time test.py
>>> t = timeit.Timer("t = foo()/nprint t")
ß
被
timeit
的代码段
>>> t.timeit()
Traceback (most recent call last):
File "<pyshell#12>", line 1, in -toplevel-
t.timeit()
File "E:/Python23/lib/timeit.py", line 158, in timeit
return self.inner(it, self.timer)
File "<timeit-src>", line 6, in inner
foo()
ß
标准输出是这样的
NameError: global name 'foo' is not defined
>>> try:
t.timeit()
except:
t.print_exc()
Traceback (most recent call last):
File "<pyshell#17>", line 2, in ?
File "E:/Python23/lib/timeit.py", line 158, in timeit
return self.inner(it, self.timer)
File "<timeit-src>", line 6, in inner
t = foo()
ß
print_exc()
的输出是这样的,方便定位错误
NameError: global name 'foo' is not defined
|