linux高精度struct timespec 和 struct timeval
一、struct timespec 定义:
typedef long time_t;
#ifndef _TIMESPEC
#define _TIMESPEC
struct timespec {
time_t tv_sec; // seconds
long tv_nsec; // and nanoseconds
};
#endif
struct timespec有两个成员,一个是秒,一个是纳秒, 所以最高精确度是纳秒。
一般由函数int clock_gettime(clockid_t, struct timespec *)获取特定时钟的时间,常用如下4种时钟:
CLOCK_REALTIME 统当前时间,从1970年1.1日算起
CLOCK_MONOTONIC 系统的启动时间,不能被设置
CLOCK_PROCESS_CPUTIME_ID 本进程运行时间
CLOCK_THREAD_CPUTIME_ID 本线程运行时间
struct tm *localtime(const time_t *clock); //线程不安全
struct tm* localtime_r( const time_t* timer, struct tm* result );//线程安全
size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );
二、struct timeval 定义:
struct timeval {
time_t tv_sec; // seconds
long tv_usec; // microseconds
};
struct timezone{
int tz_minuteswest; //miniutes west of Greenwich
int tz_dsttime; //type of DST correction
};
struct timeval有两个成员,一个是秒,一个是微秒, 所以最高精确度是微秒。
一般由函数int gettimeofday(struct timeval *tv, struct timezone *tz)获取系统的时间
#include<stdio.h>
#include<time.h>
#include<sys/time.h> void nowtime_ns()
{
printf("---------------------------struct timespec---------------------------------------\n");
printf("[time(NULL)] : %ld\n", time(NULL));
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
printf("clock_gettime : tv_sec=%ld, tv_nsec=%ld\n", ts.tv_sec, ts.tv_nsec); struct tm t;
char date_time[];
strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&ts.tv_sec, &t));
printf("clock_gettime : date_time=%s, tv_nsec=%ld\n", date_time, ts.tv_nsec);
}
void nowtime_us()
{
printf("---------------------------struct timeval----------------------------------------\n");
printf("[time(NULL)] : %ld\n", time(NULL));
struct timeval us;
gettimeofday(&us,NULL);
printf("gettimeofday: tv_sec=%ld, tv_usec=%ld\n", us.tv_sec, us.tv_usec); struct tm t;
char date_time[];
strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&us.tv_sec, &t));
printf("gettimeofday: date_time=%s, tv_usec=%ld\n", date_time, us.tv_usec);
} int main(int argc, char* argv[])
{
nowtime_ns();
printf("\n");
nowtime_us();
printf("\n");
return ;
}
#include <time.h> // 返回自系统开机以来的毫秒数(tick)
unsigned long GetTickCount()
{
struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (ts.tv_sec * + ts.tv_nsec / );
} int main()
{
struct timespec time1 = { , }; clock_gettime(CLOCK_REALTIME, &time1);
printf("CLOCK_REALTIME: %d, %d\n", time1.tv_sec, time1.tv_nsec); clock_gettime(CLOCK_MONOTONIC, &time1);
printf("CLOCK_MONOTONIC: %d, %d\n", time1.tv_sec, time1.tv_nsec); clock_gettime(CLOCK_MONOTONIC_RAW, &time1);
printf("CLOCK_MONOTONIC_RAW: %d, %d\n", time1.tv_sec, time1.tv_nsec); clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
printf("CLOCK_PROCESS_CPUTIME_ID: %d, %d\n", time1.tv_sec,
time1.tv_nsec); clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time1);
printf("CLOCK_THREAD_CPUTIME_ID: %d, %d\n", time1.tv_sec,
time1.tv_nsec); printf("\n%d\n", time(NULL)); printf("tick count in ms: %ul\n", GetTickCount()); return ;
}
linux高精度struct timespec 和 struct timeval的更多相关文章
- struct timespec 和 struct timeval
time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在 ...
- (linux)struct inode 和 struct file
转自:http://www.cnblogs.com/QJohnson/archive/2011/06/24/2089414.html 1.struct inode──字符设备驱动相关的重要结构介绍 内 ...
- struct inode 和 struct file
1.struct inode──字符设备驱动相关的重要结构介绍 内核中用inode结构表示具体的文件,而用file结构表示打开的文件描述符.Linux2.6.27内核中,inode结构体具体定义如下: ...
- struct timeval 和 struct timespec
struct timeval { time_t tv_sec; suseconds_t tv_usec; }; 測试代码例如以下: #include <stdio.h> #include ...
- Linux内核中的双向链表struct list_head
一.双向链表list_head Linux内核驱动开发会经常用到Linux内核中经典的双向链表list_head,以及它的拓展接口和宏定义:list_add.list_add_tail.list_de ...
- 转:struct sockaddr与struct sockaddr_in ,struct sockaddr_un的区别和联系
在linux环境下,结构体struct sockaddr在/usr/include/linux/socket.h中定义,具体如下:typedef unsigned short sa_family_t; ...
- linux 高精度定时器例子
//author:DriverMonkey //phone:13410905075 //mail:bookworepeng@Hotmail.com //qq:196568501 #include &l ...
- struct msghdr和struct cmsghdr【转载】
理解struct msghdr当我第一次看到他时,他看上去似乎是一个需要创建的巨大的结构.但是不要怕.其结构定义如下:struct msghdr { void *msg_name ...
- 获取网络接口信息——ioctl()函数与结构体struct ifreq、 struct ifconf
转载请注明出处:windeal专栏 Linux 下 可以使用ioctl()函数 以及 结构体 struct ifreq 结构体struct ifconf来获取网络接口的各种信息. ioctl 首先看 ...
随机推荐
- NoSQL现状
经过了至少4年的激烈争论,现在是对NoSQL的现状做一个阶段性结论的时候了.围绕着NoSQL发生了如此之多的事情,以至于很难对其作出一个简单概括,也很难判断它达到了什么目标以及在什么方面没有达到预期. ...
- java.util.Vector排序
Vector的排序: import java.util.*; class MyCompare implements Comparator //实现Comparator,定义自己的比较方法{public ...
- sharepoint 2013 网站集解锁
前言 最近碰到这样的一个问题,就是SharePoint 站点备份(Backup-SPSite)的时候,速度特别慢,然后网站变成只读状态(备份过程中只读属于正常现象).但是,自己手欠把备份命令的Powe ...
- Js与正则表达式
原本接着上面的章节,这一章节应该是写 Jquery事件的,由工作需要,暂时横插一篇正则表达式的学习,这里是边学边记录,错误之处,希望指正,提拔一二,不甚感激! 此章节有 1.1 正则表达式的定义 1. ...
- 《Linux系统编程(第2版)》
<Linux系统编程(第2版)> 基本信息 作者: (美)Robert Love 译者: 祝洪凯 李妹芳 付途 出版社:人民邮电出版社 ISBN:9787115346353 上架时间:20 ...
- .NET培训 | JAVA培训 | 最课程
最课程(www.zuikc.com) 软件开发培训,在线软件培训的创新者!我们的创新在于: 1:一次购买,终身服务.每个最课程学员都会分配一位专职教师及一位监管教师,点对点跟进课程进度,直到您学会课程 ...
- Orchard模块开发全接触7:订单与支付之Event Bus
在这部分,我们要完成的工作有: 1:将购物车内的商品变成真正的订单: 2:理解 父子及一对多关系: 3:写一个针对 Event Bus 的扩展点: 4:实现一个针对该扩展点的模拟的 支付服务: 一:创 ...
- JavaScript:String 对象
ylbtech-JavaScript:String 对象 1.返回顶部 String 对象 String 对象用于处理文本(字符串). 创建 String 对象的语法: new String(s); ...
- 全文检索(SOLR)前端应用浅析 (转)
最近在一个关于知识管理系统中检索的一个功能方案,找到了一个很好的参考应用方案嘎要的分析一下,希望有类似应用的可以交流交流. 提起全文检索,Lucene的大名估计地球人都知道,通过这么多年的发展,外围的 ...
- [leetcode]Merge Sorted Array @ Python
原题地址:https://oj.leetcode.com/problems/merge-sorted-array/ 题意:Given two sorted integer arrays A and B ...