
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to create a high resolution timer with C++ and Linux?
To create high resolution timer we can use the chrono library. This library has high resolution clock. This can count in nanoseconds.
In this program we will see the execution time in nanoseconds. We will take the time value at first, then another time value at the last, then find the difference to get elapsed time. Here we are using blank loop to pause the effect for sometimes.
Example
#include <iostream> #include <chrono> typedef std::chrono::high_resolution_clock Clock; main(){ auto start_time = Clock::now(); for(int i = 0; i<200000000; i++) { //create one empty loop ; } auto end_time = Clock::now(); std::cout << "Time difference:" << std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count() << " nanoseconds" << std::endl; }
Output
Time difference: 536395307 nanoseconds
- Related Articles
- How to create a countdown timer with JavaScript?
- How to create a multi-resolution favicon with GIMP?
- How to plot a high resolution graph in Matplotlib?
- How to create a timer using tkinter?
- Top Twenty Sites with Free High Resolution Images
- How to create timer using C++11?
- Python Program to Create a Lap Timer
- How to create a CPU spike with bash command on Linux?
- How to create a process in Linux?
- Making a countdown timer with Python and Tkinter
- How to create a zip file and ignore directory structure in Linux?
- How to detect the screen resolution with JavaScript?
- How to Create a crontab Through a Script on Linux
- How to Create a Cron Job and Execute at a Given Time in Linux
- How to Create a New Ext4 File System in Linux?

Advertisements