linux 下高精度时间
今天在公司代码中看到了使用select函数的超时功能作定时器的用法,便整理了如下几个Linux下的微秒级别的定时器。在我的Ubutu10.10 双核环境中,编译通过。
- /*
- * @FileName: test_sleep.c
- * @Author: wzj
- * @Brief:
- *
- *
- * @History:
- *
- * @Date: 2012年02月07日星期二22:20:00
- *
- */
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- #include<sys/time.h>
- #include<errno.h>
- #include<string.h>
- #include<unistd.h>
- #include<sys/types.h>
- #include<sys/select.h>
- int main(int argc, char **argv)
- {
- unsigned int nTimeTestSec = 0;
- unsigned int nTimeTest = 0;
- struct timeval tvBegin;
- struct timeval tvNow;
- int ret = 0;
- unsigned int nDelay = 0;
- struct timeval tv;
- int fd = 1;
- int i = 0;
- struct timespec req;
- unsigned int delay[20] =
- {500000, 100000, 50000, 10000, 1000, 900, 500, 100, 10, 1, 0};
- int nReduce = 0; //误差
- fprintf(stderr, "%19s%12s%12s%12s\n", "fuction", "time(usec)", "realtime", "reduce");
- fprintf(stderr, "----------------------------------------------------\n");
- for (i = 0; i < 20; i++)
- {
- if (delay[i] <= 0)
- break;
- nDelay = delay[i];
- //test sleep
- gettimeofday(&tvBegin, NULL);
- ret = usleep(nDelay);
- if(ret == -1)
- {
- fprintf(stderr, "usleep error, errno=%d [%s]\n", errno, strerror(errno));
- }
- gettimeofday(&tvNow, NULL);
- nTimeTest = (tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec - tvBegin.tv_usec;
- nReduce = nTimeTest - nDelay;
- fprintf (stderr, "\t usleep %8u %8u %8d\n", nDelay, nTimeTest,nReduce);
- //test nanosleep
- req.tv_sec = nDelay/1000000;
- req.tv_nsec = (nDelay%1000000) * 1000;
- gettimeofday(&tvBegin, NULL);
- ret = nanosleep(&req, NULL);
- if (-1 == ret)
- {
- fprintf (stderr, "\t nanousleep %8u not support\n", nDelay);
- }
- gettimeofday(&tvNow, NULL);
- nTimeTest = (tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec - tvBegin.tv_usec;
- nReduce = nTimeTest - nDelay;
- fprintf (stderr, "\t nanosleep %8u %8u %8d\n", nDelay, nTimeTest,nReduce);
- //test select
- tv.tv_sec = 0;
- tv.tv_usec = nDelay;
- gettimeofday(&tvBegin, NULL);
- ret = select(0, NULL, NULL, NULL, &tv);
- if (-1 == ret)
- {
- fprintf(stderr, "select error. errno = %d [%s]\n", errno, strerror(errno));
- }
- gettimeofday(&tvNow, NULL);
- nTimeTest = (tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec - tvBegin.tv_usec;
- nReduce = nTimeTest - nDelay;
- fprintf (stderr, "\t select %8u %8u %8d\n", nDelay, nTimeTest,nReduce);
- //pselcet
- req.tv_sec = nDelay/1000000;
- req.tv_nsec = (nDelay%1000000) * 1000;
- gettimeofday(&tvBegin, NULL);
- ret = pselect(0, NULL, NULL, NULL, &req, NULL);
- if (-1 == ret)
- {
- fprintf(stderr, "select error. errno = %d [%s]\n", errno, strerror(errno));
- }
- gettimeofday(&tvNow, NULL);
- nTimeTest = (tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec - tvBegin.tv_usec;
- nReduce = nTimeTest - nDelay;
- fprintf (stderr, "\t pselect %8u %8u %8d\n", nDelay, nTimeTest,nReduce);
- fprintf (stderr, "--------------------------------\n");
- }
- return 0;
- }
老大建议我们在对精度要求较高的情况下使用select()作为定时器,最大的好处就是不会影响信号处理,线程安全,而且精度能得到保证。在这个实验中,当时间延时时间较长时,select和pselect表现较差,当时间小于1毫秒时,他们的精确度便提高了,表现与usleep、nanosleep不相上下,有时精度甚至超过后者
linux 下高精度时间的更多相关文章
- Linux下长时间ping网络加时间戳并记录到文本
Linux下长时间ping网络加时间戳并记录到文本 由于一些原因,比如需要检查网络之间是否存在掉包等问题,会长时间去ping一个地址,由于会输出大量的信息而且最好要有时间戳,因此我们可以使用简单的 ...
- Linux下精确控制时间的函数
Linux下精确控制时间的函数 在测试程序接口运行时间的时候,常用time,gettimeofday等函数,但是这些函数在程序执行的时候是耗费时间的,如果仅仅测试时间还行,但是如果程序中用到时间控制类 ...
- Linux下设置时间
Linux下设置时间 提供两种最根本有效的方式,就是更改时区.这里以更改为国内上海时间例子,其他地方时区同理. 方法一 备份文件 mv /etc/localtime /etc/localtime.ba ...
- Linux下系统时间函数、DST等相关问题总结(转)
Linux下系统时间函数.DST等相关问题总结 下面这个结构体存储了跟时区相关的位移量(offset)以及是否存在DST等信息,根据所在的时区信息,很容易找到系统时间与UTC时间之间的时区偏移,另外根 ...
- linux下的时间
1.linux下时间管理机制: 在系统启动时,Linux操作系统将时间从CMOS中读到系统时间变量中,以后修改时间通过修改系统时间实现.为了保持系统时间与CMOS时间的一致性,Linux每隔11分钟会 ...
- linux下修改时间和时区
一.修改linux的时间在root用户下,使用date指令:date -s1.只修改日期,不修改时间,输入: date -s -- 2.只修改时间,输入: date -s :: 3.同时修改日期时间, ...
- linux下的时间管理概述
2017/6/21 时间这一概念在生活中至关重要,而在操作系统中也同样重要,其在系统中的功能绝不仅仅是给用户提供时间这么简单,内核的许多机制都依赖于时间子系统.但凡是要在某个精确的时间执行某个事件,必 ...
- Linux下RTC时间的读写分析【转】
本文转载自:http://blog.csdn.net/little_walt/article/details/52880840 Linux系统下包含两个时间:系统时间和RTC时间. 系统时间:是由主芯 ...
- LINUX下的时间与时区的设置
在RHEL下,如果只装英文版系统,设置好时区以后(上海时间,UTC) 在命令行下用date命令查看,总是与实际的北京时间差8小时,其实硬件时间都是准确的.会带来视觉不便. 今天下决心解决此问题,不过也 ...
随机推荐
- 『jQuery』.html(),.text()和.val()的概述及使用--2015-08-11
如何使用jQuery中的.html(),.text()和.val()三种方法,用于读取,修改元素的html结构,元素的文本内容,以及表单元素的value值的方法 本节内容主要介绍的是如何使用jQu ...
- 再生龙备份还原linux系统
相关下载: Clonezilla再生龙:http://sourceforge.net/projects/clonezilla/files/clonezilla_live_stable/ tuxboot ...
- 私有DockerHub搭建
docker简介 一个开源的应用容器引擎,可以用来打包程序,可以包入依赖环境,这样只需要提供docker image即可,类似于虚拟机,但是更轻量级. 几个概念: Paas,platform as a ...
- LeetCode之Weekly Contest 92
第一题:转置矩阵 问题: 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9] ...
- Thinkphp5 的常用连式查询
目录 取出表中改字符串前两位等于01的数据 按主键查询 不按主键查 JOIN方法 的左右连接 not in 方法 like 查询 where 按条件筛选查询 取出表中改字符串前两位等于01的数据 $p ...
- Python全栈之jQuery笔记
jQuery runnoob网址: http://www.runoob.com/jquery/jquery-tutorial.html jQuery API手册: http://www.runoob. ...
- python数据类型之字典(dict)和其常用方法
字典的特征: key-value结构key必须可hash,且必须为不可变数据类型.必须唯一. # hash值都是数字,可以用类似于2分法(但比2分法厉害的多的方法)找.可存放任意多个值.可修改.可以不 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- Linux学习-核心与核心模块
谈完了整个开机的流程,您应该会知道,在整个开机的过程当中,是否能够成功的驱动我们主机的硬 件配备, 是核心 (kernel) 的工作!而核心一般都是压缩文件,因此在使用核心之前,就得要将他解 压缩后, ...
- UVa 12299 线段树 单点更新 RMQ with Shifts
因为shift操作中的数不多,所以直接用单点更新模拟一下就好了. 太久不写线段树,手好生啊,不是这错一下就是那错一下. PS:输入写的我有点蛋疼,不知道谁有没有更好的写法. #include < ...