4.5.24 · HinglishSoftware Engineering

Performance profiling — CPU, memory, I - O profiling

2,019 words9 min readRead in English

4.5.24 · Coding › Software Engineering


WHY profiling exists

WHY does this matter?

  • Bottlenecks ke baare mein developer ka intuition famously bura hota hai. Donald Knuth: "Premature optimization is the root of all evil." Tum galat cheez optimize karte ho, complexity badhate ho, aur kuch hasil nahi hota.
  • 80/20 / Amdahl reality: code ka ek chhota sa hissa runtime ko dominate karta hai. Us hisse ko dhundho aur tumhe pura payoff saste mein mil jaata hai.

The three resources

1. CPU profiling — do flavours

Key CPU terms:

  • Self (exclusive) time: function ke andar ka time, jo calls kiye hain unhe exclude karke.
  • Cumulative (inclusive) time: self time plus saare descendants.
  • Ek flame graph call frames ko stack karta hai; width = time, toh sabse wide bars tumhare hotspots hain.
Figure — Performance profiling — CPU, memory, I - O profiling

2. Memory profiling

  • Tools: Python tracemalloc, memory_profiler; Valgrind/Massif (C/C++); browser heap snapshots (JS).
  • Memory leak = unreachable-but-not-freed memory (C), ya reachable-but-forgotten references (GC languages — jaise growing global cache, dangling event listeners).

3. I/O profiling

  • Classic offender: N+1 query problem — ek list fetch karo, phir har item ke liye ek extra query (1 + N queries) ek batched join ki jagah.

Wall-clock vs CPU time (sabse important distinction)


Worked examples


Common mistakes (Steel-man + fix)


Recall Feynman: 12-saal ke bachche ko samjhao

Socho tumhara homework poori shaam leta hai aur tum jaldi khatam karna chahte ho. Yeh guess karne ki jagah ki kaunsa subject slow hai, tum ek stopwatch se har ek ko time karte ho. Pata chala ki 90% time ek bade math worksheet mein jaata hai — toh tum woh fix karte ho, na woh spelling jo tum pehle se 2 minute mein khatam kar lete ho. Profiling ek program ke har hisse par stopwatch lagana hai. Teen stopwatches hain: ek "brain busy hai" ke liye (CPU), ek "desk papers se bhari padhi hai" ke liye (memory), aur ek "library book aane ka wait kar rahe ho" ke liye (I/O). Aur ek rule hai: agar slow part poori shaam ka sirf ek chhota slice hai, toh use speed up karna bahut kam help karta hai — isliye hamesha sabse bada slice pehle fix karo.


Flashcards

Profiling kya hai?
Ek running program ke resource use (CPU/memory/I/O) ka dynamic measurement jo functions/lines/call paths ke saath attributed hota hai.
Deterministic vs sampling profiler?
Deterministic har call ko hook karta hai (accurate, high overhead); sampling periodically "abhi kya run ho raha hai" poll karta hai (low overhead, statistical).
Amdahl's Law aur uski limit batao.
; jab , , toh chhota gain ko cap karta hai.
Self time vs cumulative time?
Self = function mein time callees ko exclude karke; cumulative = self + saare descendants.
CPU-bound ko I/O-bound se kaise distinguish karte ho?
Wall vs CPU time compare karo: wall ≈ CPU ⇒ CPU-bound; wall ≫ CPU ⇒ I/O-bound (waiting).
Flame graph mein bar width ka matlab?
Time (cumulative) — sabse wide bars hotspots hain.
Sampling profiler standard error formula?
for samples.
N+1 query problem kya hai?
Ek list ke liye 1 query + har item ke liye 1 extra query karna; ek query mein batching/joining se fix karo.
Deterministic profiler tumhe mislead kyon kar sakta hai?
Woh per-call overhead add karta hai, call-heavy functions ko inflate karta hai aur ranking distort karta hai; hot code ke liye sampling use karo.
Ek function runtime ka 15% hai; maximum possible speedup?
chahe instant bana do.

Connections

  • Amdahl's Law — kisi bhi optimization ki math ceiling
  • Big-O Complexity — predict karta hai kaunsa function profile karne se pehle badly scale karega
  • Flame Graphs — CPU profiles ka visualization
  • Memory Management & Garbage Collection — memory profilers kya expose karte hain
  • N+1 Query Problem — canonical I/O bottleneck
  • Benchmarking vs Profiling — totals measure karna vs hotspots locate karna
  • Observability — Logs, Metrics, Traces — production-scale profiling
  • Caching Strategies — common fix jab hotspot mil jaaye

Concept Map

fails, so use

measure first

ceiling S=1/[1-p+p/s]

find big p

three resources

three resources

three resources

hooks every call

periodic snapshots

high overhead 2-10x

low overhead, approx

sqrt of p 1-p over N

Profiling: measure resource use

Guessing bottlenecks

Amdahl's Law

80-20 reality

CPU-bound

Memory-bound

I-O-bound

Deterministic profiler

Sampling profiler

Sampling error SE