5.3.16 · D2 · HinglishBuild Systems & Toolchain

Visual walkthroughProfiling — gprof, perf, Valgrind - Callgrind

2,202 words10 min read↑ Read in English

5.3.16 · D2 · Coding › Build Systems & Toolchain › Profiling — gprof, perf, Valgrind - Callgrind

Hum parent note ka central result derive kar rahe hain: Har symbol ko use hone se pehle earn kiya jayega.


Step 1 — "Program chalana" time mein actually kaisa dikhta hai

Figure — Profiling — gprof, perf, Valgrind - Callgrind

Sach wala jawab jo hum chahte hain woh hai : literally, har red band ki lengths jod do. Ek perfect tool exactly wahi karta. Problem yeh hai: har band edge ko exactly measure karna expensive hai (yahi instrumentation hai, aur yeh timing distort karta hai). Toh hum cleverly cheat karte hain.


Step 2 — Cheat: measure mat karo, photograph karo

Figure — Profiling — gprof, perf, Valgrind - Callgrind

Picture padho: agar clicks the aur of them red band par pade, toh compute_dist ne 10 mein se 4 clicks "catch" kiye.


Step 3 — Har click length ka ek chhota ruler hai

Figure — Profiling — gprof, perf, Valgrind - Callgrind

Step 4 — Ise fraction ke roop mein likhte hain (woh form jo tum actually padhoge)

Figure — Profiling — gprof, perf, Valgrind - Callgrind

Step 5 — Yeh sirf approximate kyun hai: chhote counts ki wobble

Figure — Profiling — gprof, perf, Valgrind - Callgrind

Step 6 — Degenerate cases (reader ko kabhi stranded mat chodo)

Figure — Profiling — gprof, perf, Valgrind - Callgrind

Ek-picture summary

Upar sab kuch ek image mein collapse ho jaata hai: ek timeline, evenly-spaced red clicks, mein count kiye gaye red clicks, se divide, se multiply, self time nikalta hai. Arrows trace karo.

Figure — Profiling — gprof, perf, Valgrind - Callgrind
Recall Feynman retelling — plain words mein poora walkthrough

Apne program ke run ko ek lamba ruler imagine karo jo floor par rakkha hua hai, coloured stripes mein painted — har stripe ek function ke andar bitaya gaya time hai, aur hum jis stripe ki parwah karte hain woh red hai. Hum har stripe edge measure nahi karna chahte (bahut jhanjhat hai, aur use touch karna timing disturb karta hai). Toh iske bajaye hum ek metronome set karte hain jo har seconds mein tick karta hai. Har tick par hum neeche nazar daalte hain aur note karte hain hum kis colour par khade hain. Red ticks () aur saare ticks () count karo. Kyunki ticks evenly spaced hain, har tick floor ka ek slice "own" karta hai, toh red time ≈ (red ticks) × . Poori cheez se divide karne par gayab ho jaata hai, ek clean fraction of ticks bachta hai — wahi "% time" hai jo tumhara profiler print karta hai. Yeh sirf ek estimate hai kyunki ek tick ek patli stripe miss kar sakta hai (isi liye ek function 0% show kar sakta hai); zyada ticks lo aur wobble ki tarah shrink hoti hai; infinitely many lo aur tumhe Callgrind ka exact answer milta hai.

Recall Quick self-check

final formula se kyun gayab ho jaata hai? ::: Dono self time () aur total time () mein ka ek factor hai, toh woh ratio mein cancel ho jaata hai. Ek function 0 self samples dikhata hai — kya uska true time zero hai? ::: Nahi. Ek fast function do clicks ke beech poori tarah pad sakta hai aur miss ho sakta hai; rate badhaao ya exact-count Callgrind use karo. Percentage ki statistical error aadhi karne ke liye kitne zyada samples chahiye? ::: Roughly 4× zyada, kyunki error ki tarah scale karti hai. Is picture ka limit kis tool se correspond karta hai? ::: Callgrind — exact, deterministic counts, 20–100× slowdown par.

Related: Compiler Optimization Flags (-O2, -O3, inlining) · Cache Hierarchy & Locality of Reference · Branch Prediction & Pipelining · Debugging with gdb · Build Systems & Toolchain