Presentation is loading. Please wait.

Presentation is loading. Please wait.

Profiling for Performance in C++

Similar presentations


Presentation on theme: "Profiling for Performance in C++"— Presentation transcript:

1 Profiling for Performance in C++

2 Your Current Toolbox Big-O Analysis Guessing based on ECE244 knowledge
An O(n2) function will theoretically take longer than O(n log n) The C++ standard library provides complexity for most functions Guessing based on ECE244 knowledge “This is probably calling the copy constructor, I should pass by reference instead” Guessing based on what you think you know This is bad, but you’re not alone Using the stopwatch app on your smartphone

3 “[ECE297 students] waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.” - Donald Knuth, when asked about ECE297 students from the University of Toronto

4 Measuring Actual Performance
Manual Measurements Profiling Measurements Write code to measure time Lowest overhead (if done correctly) Works well for measuring parts of a function Use a profiler to do it all for you Some overhead The profiler will interrupt your program at some sampling rate During an interrupt it will measure key things Works well for identifying bottlenecks Find the 3%!

5 Measuring Performance Manually
Get the timestamp before the region of interest Get the timestamp after the region of interest Subtract before from after This is how long the region of interest took Save or output the time in the desired format/units Nanoseconds, microseconds, milliseconds?

6 The Performance of act_on_button_press

7 Step 1: Get the timestamp before the region of interest
Step 2: Get the timestamp after the region of interest Step 3: Subtract the before timestamp from the after timestamp Step 4: Print or save the measurement

8 Step 2: Get the timestamp after the region of interest
Step 3: Subtract the before timestamp from the after timestamp Step 4: Print or save the measurement

9 Step 3: Subtract the before timestamp from the after timestamp
Step 4: Print or save the measurement

10 Step 4: Print or save the measurement

11

12 More on std::chrono Why are we using it?
Other options: system_clock, steady_clock What does high_resolution_clock::now() return? Why do we need to do a duration_cast<…>?

13 More on std::chrono Why are we using it?
The region of interest may be very short We want the clock with the fastest sampling rate What does high_resolution_clock::now() return? A time_point Why do we need to do a duration_cast<…>? To specify our desired units (e.g. nanoseconds)

14 Saving to a File

15 Saving to a File: Design Choices
Why are we using static? Why are we using std::endl instead of “\n”? Why did we separate “act_on_button_press” from the measurement with a comma?

16 Saving to a File: Design Choices
Why are we using static? We ensure the perf object is only ever created once for the act_on_button_press function The destructor will be called when the program ends Why are we using std::endl instead of “\n”? std::endl will flush the data to the file If an exception occurs somewhere in our program, we (hopefully) don’t lose data Why did we separate “act_on_button_press” from the measurement with a comma? This is a comma-separated-value file Easily loaded into your favourite spreadsheet program

17 Saving to a Bigger File

18 Disabling Code If you comment out the first line, none of the profiling code will be compiled.

19 Measuring Performance with a Profiler
Change main.cpp Compile using the “profile” configuration netbeans (see screenshot) make CONF=profile Run mapper ./mapper Run gprof gprof mapper > info.txt

20 Profiling what you want in main.cpp
Let’s ignore draw_map and focus on M3 Select some tests and try them out The more tests you do, the more time is spent there instead of load_map If you just want to profile load_map, don’t do this

21 What does info.txt look like?

22 gprof mapper gmon.out | gprof2dot.py –s | xdot -

23 Nodes tell us… Total time spent as a percent spent in function
The “self-time” spent executing the function Does not include descendents The number of times the function was called dijkstra_no_priority_qu eue called 5 times

24 Edges tell us… Percentage of total time when the child is called by the parent Number of times the parent called the child

25 Code to Graph

26 Code to Graph Note that current_id is… The result of find_closest_node
Used to erase unvisited_node, an std::set Used to get the neighbours of an intersection, an std::unordered_map

27 Code to Graph Note that current_id is… The result of find_closest_node
Used to erase unvisited_node, an std::set Used to get the neighbours of an intersection, an std::unordered_map 2 1 3 1 2 3


Download ppt "Profiling for Performance in C++"

Similar presentations


Ads by Google