clock_gettime 用法

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <time.h>

#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;
}
posted @ 2018-08-16 09:04  苏小北1024  阅读(2021)  评论(0编辑  收藏  举报