Debugging finds incorrect behavior, tracing records execution events and profiling measures resource use or hotspots.
Understanding the Concept
Debugging finds incorrect behavior, tracing records execution events and profiling measures resource use or hotspots. These tools answer different questions and complement tests.
Start with a reproducible failure or performance target before collecting measurements.
How to interpret the measurement
Before optimizing, establish an objective measurement and a representative workload. Memory size, function-call counts, elapsed time and samples are different metrics: knowing one does not determine the others. Repeat observations before drawing a conclusion about a performance regression.
Profiling output is evidence for finding a bottleneck, not a performance guarantee. Check that the program still produces the correct result after any optimization and note version, input size and measurement conditions.
Worked Example
Read the code from the first line to the last. Identify which values are inputs, which operation is being demonstrated and what should happen before running it.
def discount(price, rate):
return price * (1 - rate)
assert discount(100, 0.10) == 90
print(discount(80, 0.25))
# Use a debugger for wrong values and a profiler for slow execution.
Walkthrough: What Each Part Does
- The definition discount creates reusable behavior. Its body runs when called, awaited or instantiated; defining it alone does not run the body of an ordinary function.
- The printed number or profile is diagnostic data. Repeat the observation and identify the unit before comparing values.
This demonstration focuses on one practical behavior. Before changing several lines, vary one input or one configuration choice and check whether the result changes for the reason you expected.
Example Output
60.0
The output above was captured by running the example in the available Python environment. The order of concurrent events, durations and platform-dependent values can legitimately differ on your computer.
Important Behavior and Edge Cases
Using a profiler to diagnose a wrong business rule or relying on print statements to prove performance.
Start with a reproducible failure or performance target before collecting measurements.
Practice Exercise
Given a wrong invoice total and a slow report, identify the first diagnostic tool for each.
Write down the result you expect before running your changed code. Check at least one normal case and one boundary or failure case. If your example starts workers or opens resources, verify how they are cleaned up.
Further reading: official documentation and API reference.
Common Beginner Mistakes
- Using a profiler to diagnose a wrong business rule or relying on print statements to prove performance.
- Comparing results from different workloads or environments as if the measurements were directly interchangeable.
Key Takeaways
- Debugging finds incorrect behavior, tracing records execution events and profiling measures resource use or hotspots.
- Start with a reproducible failure or performance target before collecting measurements.
- Practice: Given a wrong invoice total and a slow report, identify the first diagnostic tool for each.