clock_gettime 用法

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define TARGET_FILE_NAME    "/tmp/test"

int main(void)
{
    struct stat st;
    struct timespec start, end;
    unsigned long long elapse;
    int ret;

    ret = clock_gettime(CLOCK_REALTIME, &start);
    if (ret < 0) {
        fprintf(stderr, "Unable to get start timestamp\n");
        return ret;
    }

    ret = lstat(TARGET_FILE_NAME, &st);
    if (ret < 0) {
        fprintf(stderr, "Error on lstat()\n");
        return ret;
    }

    ret = clock_gettime(CLOCK_REALTIME, &end);
    if (ret < 0) {
        fprintf(stderr, "Unable to get end timestamp\n");
        return ret;
    }

    elapse = (end.tv_sec - start.tv_sec) * 1000000000 +
             (end.tv_nsec - start.tv_nsec);
    fprintf(stdout, "elapse: %lld ns\n", elapse);

    return 0;
}

转载于:https://www.cnblogs.com/muahao/p/9485310.html

你可能感兴趣的:(clock_gettime 用法)