Developing a fast and efficient program requires in-depth knowledge of various algorithms and the underlying platforms. To assist us in achieving this goal, we have access to commercial and open-source tools to profile our application. But sometimes it may happen due to budget constrain or other limitations, we may not get access to such tools to assist us in this endeavor. In such cases, we can use the existing C++ Chrono library to measure the time elapsed between multiple critical sections of our application to get an insight into our application's performance.
In the example below, we'll learn how to use the C++ Chrono library to measure elapsed time in a nanosecond, microsecond, and millisecond. The Chrono library is available since C++11 and depending upon the compiler, users need to enable modern C++ dialect. For example in gcc compiler -std=C++11 flag enables C++ 11 dialect.
For more details on the C++ Chrono library refer to this article.
#include<iostream>
#include <chrono> // C++ Chrono lib header
#include<cstdlib> // For random no. generator
#include<ctime> // Using system clock as seed to random no. generator
#include<string.h> // To use memset
// Function to add 3 arrays and store result in 4th array.
void summation_3_array_host(int* a, int* b, int* c, int* r, int size);
int main()
{
using namespace std;
int size = 1000000; // array size set to 1 million
int* a = new int[size];
int* b = new int[size];
int* c = new int[size];;
int* result = new int[size];;
memset(result, 0, sizeof(int) * size);
time_t t;
srand(time(&t)); // randon no.generator seed
// Initializing 3 arrays with random no.
for (int i = 0; i < size; ++i)
{
// assigning random value from 0 - 4194304 to array
a[i] = rand() & 4194304;
b[i] = rand() & 4194304;
c[i] = rand() & 4194304;
}
// start time measurement
chrono::time_point<chrono::system_clock> cpu_start = chrono::system_clock::now();
// Launching array summation function on host side
summation_3_array_host(a, b, c, result, size);
// end time measurement
chrono::time_point<chrono::system_clock> cpu_end = chrono::system_clock::now();
auto cpu_total_time_ns = chrono::duration_cast<chrono::nanoseconds>(cpu_end - cpu_start).count();
auto cpu_total_time_us = chrono::duration_cast<chrono::microseconds>(cpu_end - cpu_start).count();
auto cpu_total_time_ms = chrono::duration_cast<chrono::milliseconds>(cpu_end - cpu_start).count();
cout<<"CPU total execution time: "<< cpu_total_time_ns <<" nanoseconds \n";
cout<<"CPU total execution time: "<< cpu_total_time_us <<" microseconds \n";
cout<<"CPU total execution time: "<< cpu_total_time_ms <<" milliseconds \n";
delete a;
delete b;
delete c;
delete result;
return 0;
}
void summation_3_array_host(int* array1, int* array2, int* array3, int* result, int array_size)
{
for (int i = 0; i < array_size; ++i)
{
result[i] = array1[i] + array2[i] + array3[i];
}
}
After running the above code in my machine I get the below output.
CPU total execution time: 6292649 nanoseconds
CPU total execution time: 6292 microseconds
CPU total execution time: 6 milliseconds
Comments