diff --git "a/8th_members/\354\240\204\355\235\254\354\204\240/13\354\243\274\354\260\250.md" "b/8th_members/\354\240\204\355\235\254\354\204\240/13\354\243\274\354\260\250.md" new file mode 100644 index 0000000..c4e6bb6 --- /dev/null +++ "b/8th_members/\354\240\204\355\235\254\354\204\240/13\354\243\274\354\260\250.md" @@ -0,0 +1,145 @@ +## (Chapter 14) pdb 사용해보기 +#### (참고) 주요 PDB 명령어 +- n (next): 다음 라인으로 이동 +- s (step): 함수 내부로 들어감 +- c (continue): 다음 중단점까지 실행 +- p (print): 변수 값 출력 +- l (list): 현재 위치의 코드 표시 +- q (quit): 디버거 종료 + +#### test.py +```python +import pdb + +def add(n1, n2): + result = n1 + n2 + pdb.set_trace() + return result + +def subtract(n1, n2): + result = n2 - n1 + pdb.set_trace() + return result + +a = 1 +b = 2 +c = 3 +d = add(a, b) +e = subtract(c, b) +``` + +#### Result +``` +✗ python3 -m pdb test.py +> /Users/user/Documents/CPython-Guide/ch14/test.py(1)() +-> import pdb +(Pdb) n +> /Users/user/Documents/CPython-Guide/ch14/test.py(3)() +-> def add(n1, n2): +(Pdb) n +> /Users/user/Documents/CPython-Guide/ch14/test.py(8)() +-> def subtract(n1, n2): +(Pdb) n +> /Users/user/Documents/CPython-Guide/ch14/test.py(13)() +-> a = 1 +(Pdb) n +> /Users/user/Documents/CPython-Guide/ch14/test.py(14)() +-> b = 2 +(Pdb) l + 9 result = n2 - n1 + 10 pdb.set_trace() + 11 return result + 12 + 13 a = 1 + 14 -> b = 2 + 15 c = 3 + 16 d = add(a, b) + 17 e = subtract(c, b) + 18 +[EOF] +(Pdb) n +> /Users/user/Documents/CPython-Guide/ch14/test.py(15)() +-> c = 3 +(Pdb) n +> /Users/user/Documents/CPython-Guide/ch14/test.py(16)() +-> d = add(a, b) +(Pdb) s +--Call-- +> /Users/user/Documents/CPython-Guide/ch14/test.py(3)add() +-> def add(n1, n2): +(Pdb) n +> /Users/user/Documents/CPython-Guide/ch14/test.py(4)add() +-> result = n1 + n2 +(Pdb) n +> /Users/user/Documents/CPython-Guide/ch14/test.py(5)add() +-> pdb.set_trace() +(Pdb) n +> /Users/user/Documents/CPython-Guide/ch14/test.py(6)add() +-> return result +(Pdb) n +--Return-- +> /Users/user/Documents/CPython-Guide/ch14/test.py(6)add()->3 +-> return result +(Pdb) n +> /Users/user/Documents/CPython-Guide/ch14/test.py(17)() +-> e = subtract(c, b) +(Pdb) s +--Call-- +> /Users/user/Documents/CPython-Guide/ch14/test.py(8)subtract() +-> def subtract(n1, n2): +(Pdb) c +> /Users/user/Documents/CPython-Guide/ch14/test.py(11)subtract() +-> return result +(Pdb) l + 6 return result + 7 + 8 def subtract(n1, n2): + 9 result = n2 - n1 + 10 pdb.set_trace() + 11 -> return result + 12 + 13 a = 1 + 14 b = 2 + 15 c = 3 + 16 d = add(a, b) +(Pdb) n +--Return-- +> /Users/user/Documents/CPython-Guide/ch14/test.py(11)subtract()->-1 +-> return result +(Pdb) n +--Return-- +> /Users/user/Documents/CPython-Guide/ch14/test.py(17)()->None +-> e = subtract(c, b) +(Pdb) n +--Return-- +> (1)()->None +(Pdb) n +> /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/bdb.py(584)run() +-> self.quitting = True +(Pdb) n +> /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/bdb.py(585)run() +-> sys.settrace(None) +(Pdb) n +The program finished and will be restarted +> /Users/user/Documents/CPython-Guide/ch14/test.py(1)() +-> import pdb +(Pdb) q +``` + +## (Chapter 15) cProfile 사용해보기 + +(위 test.py에서 pdb 관련 부분만 주석 처리) +``` +✗ python3 -m cProfile test.py + 5 function calls in 0.000 seconds + + Ordered by: standard name + + ncalls tottime percall cumtime percall filename:lineno(function) + 1 0.000 0.000 0.000 0.000 test.py:3() + 1 0.000 0.000 0.000 0.000 test.py:3(add) + 1 0.000 0.000 0.000 0.000 test.py:8(subtract) + 1 0.000 0.000 0.000 0.000 {built-in method builtins.exec} + 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} +``` +![image](https://github.com/heehehe/CPython-Guide/assets/41580746/6fa7b923-d139-487d-9f2f-6fb9b55dd677)