C++ Concerns: Timing Code, Random Numbers, Memory
Timing Method 1 : Stop watch Measure real "wall" time from start to end Other processes may have very direct impact
Timing Method 2 : Use OS tools to track process time *nix: "time myprogram.exe" Windows: use PowerShell and: "Measure-Command { myProgram.exe | Out-default }" Wall time only!
Instrumented Timing Method 3 : 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 c_type : type of data returned by clock() typedef of long on win32 g++ CLOCKS_PER_SECOND : defined value Integral type – careful with division 1000 on w32 g++
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
High Resolution Timing C++11 <chrono> library: http://www.guyrutenberg.com/2013/01/27/using-stdchronohigh_resolution_clock-example/ May support < 1 ms timers Not on Windows
Random Numbers
Random Test Data w/CStdLib Need cstdlib, possibly ctime srand(int) to seed rand() to get random integer
Random Test Data w/CStdLib Seed with: Constant for same sequence each time Time for random sequence Seed only ONCE per program
Random Test Data w/CStdLib Want a number between 0 & 1 millionish
Random Test Data w/CStdLib Want a number between 0 & 1 millionish Bad recipes: rand() only up to 32,000 rand() * 30 can only generate multiples of 30 number = 0 repeat 30 times: number += rand() normal distribution
Random Test Data w/CStdLib Better recipe for 0-999,999 :
Random with C++11 Random library: Random Engine: Object that can generate sequences
Random with C++11 Distribution: object that maps generated values onto desired range: Distribution object operates on generator to produce value:
Stack vs Heap
Stack = limited space Values allocated on the stack have limited lifespan VERY limited space available Large arrays will crash program at start
Heap = Heap allocations (using new) live until deleted Large amounts of space available Where you need to put any large arrays