Debugging and Profiling

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/53

encourage image

There's no tags or description

Looks like no tags are added yet.

54 Terms

1
New cards

debugging

the process of identifying, isolating, and fixing bugs or errors in software code

  • fix correctness issues

  • find runtime errors

  • stack trace analtysis


2
New cards

profiling

the process of analyzing the performance of a program, focusing on resource usage such as CPU time, memory consumption, and input/output operations

  • improve performance

  • find bottlenecks

  • execution time analysis


3
New cards

syntax errors

break the language’s grammatical rules and prevent program from starting

  • caught by compiler or interpreter


4
New cards

syntax error example

writing prnt(“Hello”)

5
New cards

runtime errors

issues that occur while the program is actively running and cause it to crash

  • caught by the OS/environment


6
New cards

runtime error example

division by zero

trying to open a file that doesn’t exist

7
New cards

logic errors

the code runs and compiles without crashing, but it produces the wrong output or behavior

  • caught by programmer/user


8
New cards

logic error example

using Area = length + width

9
New cards

performance errors

slow down the system

  • caught by profiling tools


10
New cards

performance error example

using wrong search/sort algorithm

11
New cards

True or false: you should read traceback errors from top to bottom

false; bottom to top

12
New cards

bottom line of traceback

diagnosis - states the exact exception and gives a short explanation

13
New cards

underline markers of traceback

the ~ and ^ symbols highlight the exact expression causing the crash

14
New cards

call stack of traceback

tells file name, line number, and function name

15
New cards

pdb

built-in source code debugger that allows you to pause program execution, step through code line-by-line and inspect stack frames

16
New cards

method 1 of invoking pdb

insert the breakpoint() function anywhere in the script

17
New cards

method 2 of invoking pdb

insert import pdb; pdb.set_trace()

18
New cards

method 3 of invoking pdb

run the script “python -m pdb my_script.py”

19
New cards

What happens when pdb triggers?

it presents an interactive prompt

20
New cards

print (p)

(pdb)

evaluate and print the value of a variable/expression

21
New cards

next (n)

(pdb)

execute the next line in the current function without stopping function calls

22
New cards

step (s)

(pdb)

execute the current line and step into a function if one is called on that line

23
New cards

continue (c)

(pdb)

continue execution until the next breakpoint is encountered or the program ends

24
New cards

list (l)

(pdb)

show the source code around the current line

25
New cards

quit (q)

(pdb)

quit the debugger and exit the program immediately

26
New cards

help (h)

(pdb)

show a list of commands, or help for a specific command

27
New cards

logging

module that replaces print() statements to track events, troubleshoot bugs, and application metrics

  • must use “import logging”


28
New cards

logging.basicConfig()

configures the default global logger

29
New cards

DEBUG

10 - detailed information, typically of interest only when diagnosing problems

30
New cards

INFO

20 - confirmation that things are working as expected

31
New cards

WARNING

30 - an indication that something unexpected happened

32
New cards

ERROR

40 - due to a more serious problem, the software has not been able to perform some function

33
New cards

CRITICAL

50 - a serious error, indicating that the program itself may not be able to continue running

34
New cards

cProfile

bulit-in deterministic profiler designed to identify performance bottlenecks by measuring how often and how long functions exxecute

35
New cards

cProfile table columns

  • ncalls

  • tottime

  • percall

  • cumtime

  • percall

  • filename:lineno(function)


36
New cards

ncalls

(cProfile)

total number of times the function was called

37
New cards

tottime

(cProfile)

total time spent in the function including the time spent in sub-functions

38
New cards

percall (1)

(cProfile)

the average time spent per call (tottime/ncalls)

39
New cards

cumtime

(cProfile)

cumulative time spent in the function including all sub-functions

40
New cards

percall (2)

(cProfile)

the average cumulative time spent per call (cumtime/primitive calls)

41
New cards

filename:lineno(function)

the file name, line number, and name of function being analyzed

42
New cards

line_profiler

third party library that breaks down the execution time for every single line of code within a function

43
New cards

line_profiler table columns

  • line#

  • hits

  • time

  • per hit

  • % time


44
New cards

line #

(line_profiler)

the line number in your script

45
New cards

hits

(line_profiler)

number of times the specific line was executed

46
New cards

time

(line_profiler)

total time spent on that line

47
New cards

per hit

(line_profiler)

average time per execution of that line

48
New cards

% time

(line_profiler)

percentage of the total function time was spent on that line

49
New cards

memory_profiler

monitors how much RAM the program is consuming line by line

  • use “from memory_profiler import profile”


50
New cards

memory_profiler columns

  • mem usage

  • increment

  • occurrences

  • line contents


51
New cards

mem usage

(memory_profiler)

the total memory the Python process is using after the line executes

52
New cards

increment

(memory_profiler)

shows change in memory caused by the line

53
New cards

occurrences

(memory_profiler)

number of times the line was run

54
New cards

line contents

(memory_profiler)

the actual code being analyzed