Guide to Measuring C++ Code Execution Time
Learn how to accurately measure the execution time of your C++ code for performance tuning, bottleneck identification, and real-time system requirements. Understand different types of time measurements, utilize the STL chrono library, and follow a step-by-step process to time your code effectively with examples and explanations.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Things you need today Will be programming eventually Will need to download a file Then edit the file Get it to run Submit a SCREEN CAPTURE of your code and IT RUNNING!!!
CSCE-221 Measuring the Time of C++ Code Emil Thomas 02/07/19 Prof. Lupoli 9/11/2020 Zhipei Yan 9/19/19
Objective Figuring out How long (seconds/microseconds ) your algorithm takes to run. Basic Idea : Use a timer library and call its functions to, Get time before starting the algorithm/code/function Get time after the call to algorithm/code/function Find the difference in the above times (Using the library or by subtracting times obtained above) 3
Why should we time our code? Performance Tuning Finding the bottleneck part of the algorithm/code Realtime systems require less latency Estimating Time Complexity (BigO) Cases where the source code is not available But might extremely depend on hardware Measuring the scalability Say for distributed deployments 4
Different kinds of time Elapsed realtime/Wall Clock time difference between the time at which a task finishes and the time at which the task started. Includes, programmed delays I/O waiting times Multitasking delays etc.. CPU time time during which the processor is actively working on a certain task Both could be found for process using unix command top and time For timing C++ code we are interested in Elapsed real time/Wall Clock time 5
Measuring C++ code time Use STL name space std::chrono #include <chrono> using namespace std::chrono; Get the start time using steady_clock::now(); auto t1 = steady_clock::now(); What is auto? Don t abuse it. Call your C++ function here.. Get the end time; again using steady_clock::now(); auto t2 = steady_clock::now(); Find the difference and cast it to appropriate time unit auto elap1 = duration_cast<seconds>(t2 t1); auto elap2 = duration_cast<milliseconds>(t2 t1); auto elap3 = duration_cast<microseconds>(t2 t1); auto elap4 = duration_cast < nanoseconds> (t2 t1); Print using the count() method cout << elap1.count() << seconds\n ;
Code Example #include <iostream> #include <vector> #include <chrono> int main() { std::vector<int> testing{ 1, 1000, 1000000 }; for (int n : testing) { //Get time before func std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); fibo(n); //Get time after func std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); //Find duration std::chrono::nanoseconds elap = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start); std::cout << "Elapsed (ns): " << elap.count() << std::endl; } return 0; }
Code Example void nothing(int n) { } int slowSummation(int n) { int sum = 0; while (n) { } return sum; } sum += n--; //overflow when n>92 long long fibo(int n) { if (n <= 0) { } long long a = 1, b = 1; long long temp = 0; for (int i = 3; i <= n; i++) { temp = b; b += a; a = temp; } return b; } return 0;
Copying from text to Putty (ssh window) https://youtu.be/92O0ADZEG7w
Exercise to be uploaded to ecampus && Do Survey !!! Login to linux2.cs.tamu.edu using your netid and password Putty on Windows SSH on mac or Linux Go to your csce221 folder cd csce221 Copy the cpp file wget http://faculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/MeasuringTime/CompareTimes.cpp . Code the .cpp file following the instructions inside the code (shown as comments) Compile and run the code 3 times after changing N g++ -Wall -std=c++11 CompareTimes.cpp ./a.out Fill in the time obtained in the file header (at the top of your file) Rename the file as UIN_First_Last.cpp and upload to eCampus under lab4_time File format: .cpp only.
Exercise(Handout) & Google Survey Questions?
References: https://en.wikipedia.org/wiki/Elapsed_real_time http://www.cplusplus.com/reference/chrono/steady_clock/