line_profiler завершается ошибкой с ZeroDivisionError
Я пытаюсь профилировать код Python 2.7 с line_profiler,
но я получаю следующую ошибку:
$ kernprof -l -v mult_table.py
1 2 3 4
1 1 2 3 4
2 2 4 6 8
3 3 6 9 12
4 4 8 12 16
5 5 10 15 20
6 6 12 18 24
7 7 14 21 28
8 8 16 24 32
9 9 18 27 36
10 10 20 30 40
11 11 22 33 44
12 12 24 36 48
13 13 26 39 52
14 14 28 42 56
15 15 30 45 60
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
2 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42
3 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63
Wrote profile results to mult_table.py.lprof
Timer unit: 1e-06 s
Total time: 0 s
File: mult_table.py
Function: print_header at line 4
Traceback (most recent call last):
File "/bin/kernprof", line 11, in <module>
sys.exit(main())
File "/usr/lib/python2.7/site-packages/kernprof.py", line 230, in main
prof.print_stats()
File "/usr/lib/python2.7/site-packages/line_profiler.py", line 117, in print_stats
show_text(lstats.timings, lstats.unit, stream=stream, stripzeros=stripzeros)
File "/usr/lib/python2.7/site-packages/line_profiler.py", line 227, in show_text
show_func(fn, lineno, name, stats[fn, lineno, name], unit, stream=stream, stripzeros=stripzeros)
File "/usr/lib/python2.7/site-packages/line_profiler.py", line 201, in show_func
'%5.1f' % (100*time / total_time))
ZeroDivisionError: float division by zero
Что я могу сделать, чтобы заставить line_profiler работать?
Код Python:
$ cat mult_table.py
#!/usr/bin/env python
@profile
def print_header(n, digits_m):
spaces = digits_m + 1
print("%*s" % (digits_m, " ")),
for i in range(1, n+1):
print("%*d" % (spaces, i)),
print
@profile
def print_mult_table(n, m):
digits_n = len(str(n))
digits_m = len(str(m))
spaces_m = digits_m + 1
print_header(n, digits_m)
for j in range(1, m+1):
print("%*d" % (digits_m, j)),
for k in range(1, n+1):
print("%*d" % (spaces_m, k*j)),
print
if __name__ == "__main__":
n = 4
m = 15
print_mult_table(n, m)
print
n = 21
m = 3
print_mult_table(n, m)