首页 > 系统服务 > 详细

(二)linux的计时函数

时间:2018-06-04 10:56:06      阅读:220      评论:0      收藏:0      [点我收藏+]

linux的计时函数,用于获取当前时间。

gettimeofday()

函数 结构体 精度
time() time_t s
gettimeofday() struct timeval us

计时只使用gettimeofday()函数来获取当前时间:

  • time()函数精度太低,gettimeofday()函数以微秒为单位,可获取us/ms/s的精度,足以满足日常计时需要。

redis中的计时封装函数

/*
 * util_time.h
 *
 *  Created on: 2018-6-4
 *      Author: 
 */

#ifndef UTIL_TIME_H_
#define UTIL_TIME_H_

#include <sys/time.h>

/* Return the UNIX time in microseconds */
long long ustime(void) {
    struct timeval tv;
    long long ust;

    gettimeofday(&tv, NULL);
    ust = ((long long)tv.tv_sec)*1000000;
    ust += tv.tv_usec;
    return ust;
}

/* Return the UNIX time in milliseconds */
long long mstime(void) {
    return ustime()/1000;
}

// #define UTIL_TIME_TEST
#ifdef UTIL_TIME_TEST
#include <unistd.h>
#include <iostream>

int main()
{
    long long llStart = mstime();
    sleep(2);
    long long llEnd=mstime();
    std::cout<<llEnd-llStart<<"ms"<<std::endl;

    return 0;
}
#endif

#endif /* UTIL_TIME_H_ */

(二)linux的计时函数

原文:https://www.cnblogs.com/walkinginthesun/p/9131485.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!