clock_gettime , linux tick
clock_gettime example 사용 예제 CentOS8 에서 테스트 함
/*------------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
typedef unsigned long long int tick64_t;
typedef unsigned long int tick32_t;
tick32_t get_tick_count();
tick64_t get_tick_count64();
int main()
{
tick64_t tick64;
tick32_t tick32;
tick64 = get_tick_count64();
tick32 = get_tick_count();
printf("tick64 = %lld\n", tick64);
printf("tick32 = %lld\n", tick32);
return 0;
}
tick32_t get_tick_count()
{
tick32_t tick = 0ul;
#if defined(WIN32) || defined(WIN64)
tick = GetTickCount();
#else
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
tick = (tp.tv_sec*1000ul) + (tp.tv_nsec/1000ul/1000ul);
#endif
return tick;
}
tick64_t get_tick_count64()
{
tick64_t tick = 0ull;
#if defined(WIN32) || defined(WIN64)
tick = GetTickCount64();
#else
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
tick = (tp.tv_sec*1000ull) + (tp.tv_nsec/1000ull/1000ull);
#endif
return tick;
}
/*------------------------------------------------------------------------------*/
결과