Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Concerns: Timing Code, Random Numbers, Memory

Similar presentations


Presentation on theme: "C++ Concerns: Timing Code, Random Numbers, Memory"— Presentation transcript:

1 C++ Concerns: Timing Code, Random Numbers, Memory

2 Timing Method 1 : Stop watch
Measure real "wall" time from start to end Other processes may have very direct impact

3 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!

4 Instrumented Timing Method 3 : instrument code
Add code to record elapsed processor time between points in code Targeted timing

5 <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++

6 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

7 High Resolution Timing
C++11 <chrono> library: May support < 1 ms timers Not on Windows (or in VM if host OS is windows)

8 Random Numbers

9 Random Test Data w/CStdLib
Need cstdlib, possibly ctime srand(int) to seed rand() to get random integer

10 Random Test Data w/CStdLib
Seed with: Constant for same sequence each time Time for random sequence Seed only ONCE per program

11 Random Test Data w/CStdLib
Want a number between 0 & 1 millionish

12 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

13 Random Test Data w/CStdLib
Better recipe for 0-999,999 :

14 Random with C++11 Random library:
Random Engine: Object that can generate sequences

15 Random with C++11 Distribution: object that maps generated values onto desired range: Distribution object operates on generator to produce value:

16 Stack vs Heap

17 Stack = limited space Values allocated on the stack have limited lifespan VERY limited space available Large arrays will crash program at start

18 Heap = Heap allocations (using new) live until deleted
Large amounts of space available Where you need to put any large arrays


Download ppt "C++ Concerns: Timing Code, Random Numbers, Memory"

Similar presentations


Ads by Google