

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use clock() function in C++
Here we will see how to use the clock() in C++. This clock() is present in the time.h or ctime header file. Here we will find the elapsed time of a process using this clock() function
To get the elapsed time, we can get the time using clock() at the beginning, and at the end of the taks, then subtract the values to get the differences. After that we will divide the difference by CLOCK_PER_SEC (Number of clock ticks per second) to get the processor time.
Example
#include <iostream> #include <ctime> using namespace std; void take_enter() { cout << "Press enter to stop the counter" <<endl; while(1) { if (getchar()) break; } } main() { // Calculate the time taken by take_enter() clock_t t; t = clock(); cout << "Timer starts\n"; take_enter(); cout << "Timer ends \n"; t = clock() - t; double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time cout << "The program took "<< time_taken <<" seconds to execute"; }
Output
Timer starts Press enter to stop the counter Timer ends The program took 3.546 seconds to execute
- Related Questions & Answers
- How to use Clock in Java 8?
- How to use Lambda Function in Python?
- How to use deleteOne() function in MongoDB?
- How to use function Alias in PowerShell?
- How to Use Word() Function in Arduino?
- How to use Contains() function in Golang?
- How to use ContainsAny() function in Golang?
- How to use setTimeout() function in JavaScript?
- Convert time from 24 hour clock to 12 hour clock format in C++
- How to create digital clock with textview in android?
- How to use setInterval function call in JavaScript?
- How to use the Fields() function in Golang?
- How to use Oracle aggregate function XMLAGG ?
- Clock tickMinutes() method in Java
- Clock withZone() method in Java
Advertisements