Linux获取纳秒级别时间

  1. 在 Linux 系统中可以使用 gettimeofday 函数来获取时间,这个函数能够以毫秒的精度来获取时间
cpp 复制代码
struct timeval tv;

gettimeofday(&tv, NULL);

time_t cur_time = tv.tv_sec;

long cur_time_ms = tv.tv_usec/1000;

printf("cur_time = %d \n", cur_time);

printf("cur_time_ms = %ld \n", cur_time_ms);
  1. Linux 系统可以使用 clock_gettime 函数来获取时间精度,其可以以纳秒的精度获取时间
cpp 复制代码
struct timespec ts;

clock_gettime(CLOCK_REALTIME, &ts);

Time_t cur_time = ts.tv_sec;

long cur_time_us = ts.tv_nsec/1000;

printf("cur_time = %d \n", cur_time);

printf("cur_time_us = %ld \n", cur_time_us);
相关推荐
lThE ANDE3 小时前
最完整版Linux安装Redis(保姆教程)
linux·运维·redis
郝亚军6 小时前
ubuntu通过samba,让win11可以访问其共享文件夹
linux·服务器·ubuntu
一个人旅程~6 小时前
旧电脑的“拯救者”?Linux Mint20.3是怎样适配软件硬件以及兼顾兼容与性能的平衡的?
linux·经验分享·电脑
农村小镇哥6 小时前
nginx服务器的介绍
运维·服务器·nginx
小夏子_riotous7 小时前
Docker学习路径——3、常用命令
linux·运维·服务器·学习·docker·容器·centos
其实防守也摸鱼7 小时前
无线网络安全---WLAN相关安全工具--kali(理论附题目)
linux·安全·web安全·学习笔记·kali·命令模式·wlan
uesowys9 小时前
CentOS Linux安装部署OpenClaw
linux·centos·安装部署openclaw
IMPYLH9 小时前
Linux 的 rm 命令
linux·运维·服务器·网络·bash
YIN_尹9 小时前
【Linux系统编程】进程地址空间
linux·c++
white-persist10 小时前
【vulhub shiro 漏洞复现】vulhub shiro CVE-2016-4437 Shiro反序列化漏洞复现详细分析解释
运维·服务器·网络·python·算法·安全·web安全