Often, to check the performance of our applications we need to time the operation of some sections to identify bottlenecks and areas where the app is taking a relatively long time to do things. The challenge is, how can we calculate speed of our function in C++ when they might only take a couple of milliseconds? An execution time of one hundred milliseconds sounds fast – but it’s not great when that routine has to be called many thousands of times. How can we calculate speed of a function if it runs in such a tiny fraction of a second?
Table of Contents
What is the challenge of timing very small intervals?
As you know, there are two types of programming languages: interpreted and non-Interpreted (Compiled). All computers fundamentally work at the base level with machine code; code that can be directly programmed by assembler codes. The machine code is the lowest level of instructions which tell the computer what to do. This is the most native and fastest code, but it requires writing many lines for simple things and is hard to generalize for all kind of machines. A compiler, such as a C or C++ compiler, is a computer program that converts one high level programming language such as C/C++ code, written as text into executable machine code with a linker. Such code may not be as fast as assembler code, but the difference in speed is very small because both machine code and compiler-based code in text form are much more compatible with other CPU/GPUs and/or with other Operating Systems when you compile them on a machine. This is one reason why C++ is the fastest and most powerful programming language.
Why would I need to time my program’s functions?
Sometimes the functions that we use in our programming language are important to analyze in a scientific way – to get the facts rather than act on a hunch that “this bit feels like it might be the problem”. Sometimes you may have two different methods or algorithms and you need to objectively decide which one of them is the better choice. If the quality is about the same, common sense dictates we should always choose the fastest one. In this step, you must calculate speed of your functions.
How can I use the time library to time my C++ code execution speed?
Time Library (time.h) has a clock method to get timer of your device clock in milliseconds. This library can be used in both C & C++ applications. There is a very simple clock example in official dockwiki here. Let’s look at this example,
As you see here we use time.h library and clock_t datatype to define start and end of timing. We can get timer in miliseconds by using clock() method as below,
How can I use the chrono Library <chrono> to time and measure my C++ app functions?
Chrono Library (std::chrono) comes with C++ std and has features to get ticks , time, date and many other date and time related operations. The chrono
library defines three main types as well as utility functions and common typedefs.
- clocks
- time points
- durations
In our C++ example we will use this library to count our fastest function. This bool function might be the fastest function if we don’t consider an empty void function.
Chrono Library methods can be used to obtain a clock timer as below,
Generally, functions happen in a few milliseconds, it may be unable to calculate speed. Here the best way is calling the same function n times. i.e. 1000 or 1 Million times, so we can exactly calculate the speed of that function. This may help us to improve our functions, algorithms and / or methods to use.
We can calculate the speed of a single function() by calculating the duration 1 million times. To find the exact duration time of this function we should subtract the duration of empty same for loop. So, we should calculate this too. Finally, the difference is the exact time of 1M times of repeat. If we divide this duration to 1M we can find the duration of single function. Repeating these 5, 10 or 100 times and taking average of it would be the best result. Here is the full example to calculate a function.
As in this bool function example, you can test integer, float and double functions and any of your functions, including recursive or other functions.
Can I also use a VCL or FMX Library to analyze execution speeds of my programs?
Yes, you can use the GetTickCount()
function for this purpose.