Last active
December 12, 2015 10:09
-
-
Save Zaxuhe/4757305 to your computer and use it in GitHub Desktop.
Linux get time sample, using CLOCK_MONOTONIC_RAW
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Zaxuhe | |
**/ | |
#include <iostream> | |
#include <time.h> | |
#define SEC2NANO 1000000000.0f | |
using namespace std; | |
int main() | |
{ | |
timespec ts; | |
timespec ts2; | |
cout << "Hello world!" << endl; | |
clock_gettime(CLOCK_MONOTONIC_RAW,&ts); | |
double v1 = ts.tv_nsec ; | |
double v2 = ts.tv_sec ; | |
std::cout << ts.tv_nsec << std::endl; | |
std::cout << ts.tv_sec << std::endl; | |
usleep(400000); | |
clock_gettime(CLOCK_MONOTONIC_RAW,&ts2); | |
std::cout << ts2.tv_nsec << std::endl; | |
std::cout << ts2.tv_sec << std::endl; | |
v1 = ts2.tv_nsec - v1; | |
v2 = ts2.tv_sec - v2; | |
//std::cout << v2*SEC2NANO+v1 << std::endl; | |
long long totaltime = ((ts2.tv_sec*SEC2NANO + ts2.tv_nsec) - (ts.tv_sec*SEC2NANO + ts.tv_nsec)); | |
std::cout << totaltime/SEC2NANO << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CLOCK_MONOTONIC_RAW is specified in linux, can use CLOCK_MONOTONIC instead in Windows or lower version linux.