Skip to content

Instantly share code, notes, and snippets.

@traversaro
Last active December 24, 2022 05:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save traversaro/e031b324dd278acbd033059e3604ed3f to your computer and use it in GitHub Desktop.
Save traversaro/e031b324dd278acbd033059e3604ed3f to your computer and use it in GitHub Desktop.
Test of system clocks resolution
cmake_minimum_required(VERSION 3.5)
project(time_res_check)
set(CMAKE_CXX_STANDARD 11)
add_executable(${PROJECT_NAME} ${PROJECT_NAME}.cpp)
#include <chrono>
#include <ctime>
#include <iostream>
void print_posix_resolution(clockid_t clockid)
{
struct timespec res;
res.tv_sec = 0;
res.tv_nsec = 0;
int retVal;
retVal = clock_getres(clockid, &res);
int errsv = errno;
std::cerr << " clock_getres return value: " << retVal << " with errno " << errsv << ", " << std::endl;
std::cerr << " resolution: " << res.tv_nsec << " nanoseconds." << std::endl;
}
template<typename T>
void print_cxx11_resolution()
{
double secToNano = 1e9;
double periodInNs = secToNano*((double)T::num)/((double)T::den);
std::cerr << " resolution: " << periodInNs << " nanoseconds." << std::endl;
}
int main(int argc, char **argv)
{
std::cerr << "--> Testing POSIX APIs" << std::endl;
std::cerr << " Error macros values: " << std::endl;
std::cerr << " EINTR : " << EINTR << std::endl;
std::cerr << " EINVAL: " << EINVAL << std::endl;
std::cerr << " ENOTSUP: " << ENOTSUP << std::endl;
std::cerr << " Testing CLOCK_MONOTONIC " << std::endl;
print_posix_resolution(CLOCK_MONOTONIC);
std::cerr << " Testing CLOCK_REALTIME " << std::endl;
print_posix_resolution(CLOCK_REALTIME);
std::cerr << " Testing CLOCK_PROCESS_CPUTIME_ID " << std::endl;
print_posix_resolution(CLOCK_PROCESS_CPUTIME_ID);
std::cerr << " Testing CLOCK_THREAD_CPUTIME_ID " << std::endl;
print_posix_resolution(CLOCK_THREAD_CPUTIME_ID );
std::cerr << "--> Testing C++ APIs" << std::endl;
std::cerr << " Testing std::chrono::high_resolution_clock " << std::endl;
print_cxx11_resolution<std::chrono::high_resolution_clock::period>();
std::cerr << " Testing std::chrono::system_clock " << std::endl;
print_cxx11_resolution<std::chrono::system_clock::period>();
std::cerr << " Testing std::chrono::steady_clock " << std::endl;
print_cxx11_resolution<std::chrono::steady_clock::period>();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment