Start by clarifying the program's behavior and performance goals, then systematically profile using tools like cProfile and line_profiler to identify hotspots. Apply targeted optimizations (algorithmic, data structure, or I/O) and measure the impact with benchmarks, reporting before/after metrics.
Pro tip: Always measure before optimizing—premature optimization can waste time and introduce bugs. Use a profiler to get objective data rather than guessing.
Clarify the program's expected behavior, input size, and performance requirements. Reproduce the slowness with a representative workload to establish a baseline.
Use profiling tools like cProfile, line_profiler, or py-spy to identify where time is spent. Focus on the most time-consuming functions or lines.
Examine the hotspots to determine root causes: inefficient algorithms, unnecessary computations, poor data structures, or I/O waits. Form hypotheses for improvements.
Implement optimizations such as better algorithms, caching, vectorization, or concurrency. Make one change at a time and re-profile to validate impact.
Benchmark the optimized version against the baseline using consistent metrics (e.g., runtime, memory). Report the before/after improvement and any trade-offs.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.