Skip to content

Instantly share code, notes, and snippets.

@eduardoaugustojulio
Last active February 6, 2020 14:18
Show Gist options
  • Save eduardoaugustojulio/eefe8dcb15e696de4ff125f3498f783c to your computer and use it in GitHub Desktop.
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 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