80 likes | 227 Views
Timing Code & Profiling. Timing. Method 1 : Stop watch Measure real "wall" time from start to end Other processes may have very direct impact. Instrumented Timing. Method 2 : instrument code Add code to record elapsed processor time between points in code Targeted timing. < ctime >.
E N D
Timing • Method 1 : Stop watch • Measure real "wall" time from start to end • Other processes may have very direct impact
Instrumented Timing • Method 2 : instrument code • Add code to record elapsed processor time between points in code • Targeted timing
<ctime> • <ctime> library includes • clock() : units of time since program launch • 1ms with win32 g++ • c_type : type of data returned by clock() • typedef of long on win32 g++ • CLOCKS_PER_SECOND : defined value • Integral type – careful with division
Timing Issues • On Windows, clock() measures wall time elapsed • On linux processor time elapsed • Hard to measure short events • Wrap in a loop and average
Short Events • If loop dominates work being done • Compare loop with and without body
Code Profiling • Method 3 : Profiling • Run time analysis of code execution • Call graph • Track each function call, where it came from • Sampling • Periodically stop program, check where it was
gprof • gprof : Gnu Profiler • Simple profiling tool that ships with mingw compiler suite • Limitation • Call times are estimates – assume every function call equally expensive • Only measures time spent by your process • Ignores time spent in syscalls