Last active
February 6, 2020 14:18
-
-
Save eduardoaugustojulio/eefe8dcb15e696de4ff125f3498f783c to your computer and use it in GitHub Desktop.
Comparison of implementation of get timestamp in microsecond C between C ++11.
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
/*This gist only compare C way to get a timestamp clock_monotonic against C++11 way.*/ | |
#include <iostream> | |
#include <time.h> | |
#include <chrono> | |
#include <ctime> | |
void get_clock_time_us_time_h(unsigned long &val){ | |
struct timespec ts; | |
clock_gettime(CLOCK_MONOTONIC, &ts); | |
val = (ts.tv_sec * 1000000 + ts.tv_nsec / 1000); | |
} | |
void get_clock_time_us_std_time(unsigned long &val){ | |
val = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now()).time_since_epoch().count(); | |
} | |
int main(){ | |
unsigned long time = 0; | |
get_clock_time_us_time_h(time); | |
std::cout << time << std::endl; | |
time = 0; | |
get_clock_time_us_std_time(time); | |
std::cout << time << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment