'clock_gettime'에 해당되는 글 1건

  1. 2013.03.12 C++ nano time

C++ nano time

ITWeb/개발일반 2013.03.12 11:35

Reference URL : http://www.users.pjwstk.edu.pl/~jms/qnx/help/watcom/clibref/qnx/clock_gettime.html


get the current time of a clock


#include <time.h>
int clock_gettime( clockid_t clock_id,
                   struct timespec *tp );


The clock_gettime() function gets the current time of the clock specified by clock_id, and puts it into the buffer pointed to by tp. The only supported clock ID is CLOCK_REALTIME.

The tp parameter points to a structure containing at least the following members:

time_t tv_sec
The number of seconds since 1970.
time_t tv_nsec
The number of nanoseconds expired in the current second. This value increases by some multiple of nanoseconds, based on the system clock's resolution.


An error occurred. errno is set to indicate the error.


The clock_id is not CLOCK_REALTIME.


 * This program calculates the time required to
 * execute the program specified as its first argument.
 * The time is printed in seconds, on standard out.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

#define BILLION  1000000000L;

int main( int argc, char **argv )
    struct timespec start, stop;
    double accum;

    if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
      perror( "clock gettime" );
      exit( EXIT_FAILURE );

    system( argv[1] );

    if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) {
      perror( "clock gettime" );
      exit( EXIT_FAILURE );

    accum = ( stop.tv_sec - start.tv_sec )
          + ( stop.tv_nsec - start.tv_nsec )
            / BILLION;
    printf( "%lf\n", accum ); 

    return( EXIT_SUCCESS );

[Prototype Code]

int64_t getUniqId() {

    timeb tb;

    ftime( &tb );

    struct timespec ts;

    clock_gettime(CLOCK_REALTIME, &ts);

    int64_t uniqId = tb.millitm + (tb.time * 1000) + (ts.tv_nsec * 1000000000);

    return uniqId;


크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback 0 : Comment 0

티스토리 툴바