Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to get time in milliseconds using C++ on Linux?
Here we will see how to get time (the elapsed time for the program or any other kind of time).
Here we are using linux library for C++. There is a structure called timeval. This timeval stores the time in seconds, milliseconds. We can create two time for start and end, then find the difference from them.
Example
#include#include #include using namespace std; main() { struct timeval start_time, end_time; long milli_time, seconds, useconds; gettimeofday(&start_time, NULL); cout > ch; gettimeofday(&end_time, NULL); seconds = end_time.tv_sec - start_time.tv_sec; //seconds useconds = end_time.tv_usec - start_time.tv_usec; //milliseconds milli_time = ((seconds) * 1000 + useconds/1000.0); cout Output
Enter something: h Elapsed time: 2476 milliseconds
Advertisements
