Skip to content
BENIBIX Learn

Profiling

Development⚙️ Compilers & RuntimesPerformance

Also known as Profiler, Code Profiling, Performance Profiling

Finding the parts of a program that take the most time.

Think of it like

Think of it like a stopwatch on each runner in a race. You see who slows the team down.

Example

Lucy's page takes 6 seconds to load. The profiler shows one function that resizes images takes 5 of them. She fixes just that one.

Why it matters

People are bad at guessing what is slow. A profile shows the truth in a few minutes.

Definition

Profiling is measuring where a program spends its time or memory while it runs. A tool called a profiler watches the program and counts how long each function takes. The report shows which parts are slow and which barely matter. Often one small part takes most of the time. Profiling first means you fix the real slow spot instead of guessing.

Example code

console
$ python3 -m cProfile -s cumtime slow.py
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.500    0.500 slow.py:2(load)
        1    0.000    0.000    0.100    0.100 slow.py:4(draw)
The profiler shows load takes half a second, five times longer than draw.

Common mistake

Speeding up code before measuring it. Hours go into parts that were never the problem.

Key takeaways

  1. 1
    Measure first. Then change the code.
  2. 2
    Time and memory. Per function.
  3. 3
    Find the big one. One part often costs most.

Compare