C++Builder How To Make A Millisecond Timer In C And C++

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,203
Credits
4,034
How To Make A Millisecond Timer In C And C++
By Yilmaz Yoru December 23, 2021

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?

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 Для просмотра ссылки Войди или Зарегистрируйся. Let’s look at this example,
C++:
#include <time.h>
#include <stdio.h>
#include <windows.h>
int main(void)
{
   clock_t start, end;
   start = clock();
   Sleep(2000);
   end = clock();
   printf("The time was: %f\n", (end - start) / CLK_TCK);
   return 0;
}
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,
C++:
start = clock();
end = clock();

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.
C++:
static bool my_bool()
{
  return(true);
}
Chrono Library methods can be used to obtain a clock timer as below,
C++:
auto start = std::chrono::steady_clock::now();
// .... your function, algorithm or method here
auto end = std::chrono::steady_clock::now();
auto diff = end - start;
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.
C++:
#include <iostream>
#include <chrono>
 
static bool my_bool()
{
  return(true);
}
#define N 1000000
 
int main()
{
 auto start1 = std::chrono::steady_clock::now();
 for(int i=1; i<=N; i++);
 auto end1 = std::chrono::steady_clock::now();
 
 auto diff1 = end1 - start1;
 std::cout << "No Function Loop : " << std::chrono::duration<double, std::milli> (diff1).count() << " ms\n";
 
 auto start2 = std::chrono::steady_clock::now();
 for(int i=1; i<=N; i++) my_bool();
 auto end2 = std::chrono::steady_clock::now();
 
 auto diff2 = end2 - start2;
 std::cout << "Boolean Function With Loop : " << std::chrono::duration<double, std::milli> (diff2).count() << " ms\n";
 
 auto diff =  std::chrono::duration<double, std::milli> (diff2).count()
 -std::chrono::duration<double, std::milli> (diff1).count();
 
 std::cout << "Boolean Function Without Loop : " << diff << " ms\n";
 
 std::cout << "Speed of a Bool Function " << diff/N << " ms\n";
 
 getchar();
 return 0;
 }
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.