2018년 5월 2일 수요일

clock_gettime

clock_gettime


Posix - clock_gettime()
https://linux.die.net/man/3/clock_gettime

CLOCK_REALTIME
System-wide realtime clock. Setting this clock requires appropriate privileges.
CLOCK_MONOTONIC
Clock that cannot be set and represents monotonic time since some unspecified starting point.
CLOCK_PROCESS_CPUTIME_ID
High-resolution per-process timer from the CPU.
CLOCK_THREAD_CPUTIME_ID
Thread-specific CPU-time clock.


OSX - clock_get_time()
https://stackoverflow.com/a/11681069

  • SYSTEM_CLOCK returns the time since boot time;
  • CALENDAR_CLOCK returns the UTC time since 1970-01-01;
  • REALTIME_CLOCK is deprecated and is the same as SYSTEM_CLOCK in its current implementation.

 


예제


#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#if defined(__APPLE__) || defined(__MACH__)
#include <mach/clock.h>
#include <mach/mach.h>
#endif

int main(int argc, char *argv[])
{

#if _POSIX_TIMERS > 0
    struct timespec spec;
    clock_gettime(CLOCK_MONOTONIC, &spec);
    printf("%ld.%ld\n", spec.tv_sec, spec.tv_nsec);
#elif defined(__APPLE__) || defined(__MACH__)
    clock_serv_t cclock;
    mach_timespec_t mts;
    host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
    clock_get_time(cclock, &mts);
    mach_port_deallocate(mach_task_self(), cclock);
    printf("%d.%d\n", mts.tv_sec, mts.tv_nsec);
#else
    fprintf(stderr, "posix timer not supported platform\n");
#endif
    
    return 0;
}




댓글 없음:

댓글 쓰기