Timer implementation in C++

A C++ timer is a mechanism used to measure time intervals, track the duration of operations, or create delays in a program. Timers can be used for various purposes, such as implementing time-based functionalities, controlling animation, measuring the execution time of algorithms, and managing tasks at specific intervals.

Example:

Let's take a program for illustrating the Timers in C++.

Output:

Timer implementation in C++

Explanation:

Include statements:

  • Header files like "iomanip" is used for manipulators that format the output.
  • iostream is used to include the input and output stream operations.
  • cstdlib header file contains functions involving memory allocation and random numbers.
  • h header file is for POSIX operating system APIs. It contains the sleep() function.

timerDisplay() function:

  • This function is used to show the current time on the console.
  • In this function, we use the statement system("cls"), which is used to clear the console. Before printing the current statements, it will remove the console and print the new statement.
  • The current hours, minutes, and seconds are displayed by using the cout

timerFunction function:

This function contains the main program code, which implements the timer functionality. It includes an infinite loop, a while loop that keeps running to update the timer. It calls another function named timerDisplay(), which prints the updated time. After that, the program sleeps for one second using the function sleep(1), and then seconds are incremented, and if seconds reach 60, they reset to 0, and minutes are incremented. Similar logic is applied for minutes and hours.

Main() function:

This function initializes the timer by calling timerFunction(). The program will keep running indefinitely displaying the timer in the console until manually interrupted.

Example:

Let's take another program for illustrating the Timers in C++.

Output:

Timer implementation in C++

Explanation:

This program also implements the C++ timer. Now, we see the explanation for the above program. This function contains 3 variables, hours, minutes, and seconds, which represent the number of hours, minutes, and seconds in the timer, respectively.

There are two functions in the program: timerDisplay and main function.

timerDisplay function:

This function takes the 3 arguments, and its return type is void. The function is responsible for displaying the current time in the format HH: MM: SS. It uses setw(2) and setfill('0') to ensure that the hours, minutes, and seconds are displayed with leading zeros if they are less than 10.

main function:

Its return type is int. Here, hours, minutes and seconds are initialized to zero. It contains a while loop where the time is incremented, and it contains a sleep function that pauses the program for one second using the chrone library. If seconds reach 60, it is reset to 0, and minutes is incremented. If minutes get 60, they are reset to 0, and hours are incremented. The Display function calls the timerDisplay function to display the current time in HH: MM: SS format.

Conclusion:

In conclusion, the presented C++ timer implementations illustrate two different approaches for measuring time intervals. The first approach utilizes system calls and operating system functions like system("cls") and sleep(1) to create a simple timer, displaying hours, minutes, and seconds. This method offers a basic understanding of timekeeping and console manipulation.

The second approach employs the C++ chrono library, providing a more precise and elegant solution. Utilizing std::chrono for time manipulation and this_thread::sleep_for(chrono::seconds(1)) for delays, this method demonstrates modern C++ techniques for timer implementation, enhancing accuracy and readability.






Latest Courses