Debug&Profile

Debugging

pdb tutorial

(ipdb has a more friendly user interface)

list source code

1
2
3
4
5
6
7
8
$ python3 -m pdb bar.py
-> length = 10000000
(Pdb) l
1 -> length = 10000000
2 for i in range(length):
3 print("percentage :" , round(i/length , 2) , end='')
4 if i != length - 1:
5 print("" , end='\r')

execute line by line

1
2
3
4
5
6
pdb> s
pdb> step
# step one line
pdb> n
pdb> next
# next one line

restart and continue

1
2
3
4
pdb> restart
pdb> r # return, Continue execution until the current function returns
pdb> continue
pdb> c

print

1
2
pdb> p varname
pdb> p locals() # print all local variables

Static analyze tools

  • pyflakes can do source code checking
  • mypy can do the same job

Most editors and IDEs support displaying the output of these tools within the editor itself, highlighting the locations of warnings and errors. This is often called code lintingand it can also be used to display other types of issues such as stylistic violations or insecure constructs.

There are even tools for your English writing : writegood

Profiling

Time

1
2
3
4
$ time sleep 3
sleep 3 0.00s user 0.00s system 0% cpu 3.015 total
$ time ls
ls 0.00s user 0.00s system 77% cpu 0.007 total
  • total(real) time is the time elapsed in real world (The time you need to wait before you get your result)
  • cpu time is the time your program occupy the CPU (relating to scheduling algorithm)
  • user time is time spent executing user-level code
  • system time is time spent executing kernal-level code

Tools

  • kernprof gives time-cost line by line
  • perf (really powerful !)
  • lsof

find more at notes