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);
相关推荐
XF小冯2 小时前
Drippingblues靶机通关教程
linux·运维·网络
跟我聊天我会闯红灯5 小时前
如何开发一个运行在windows系统服务器上的服务
运维·服务器·windows
ZPC82105 小时前
参数服务器 server and client
服务器·qt
EasyGBS5 小时前
20250808:EasyGBS 对接大华 ICC 平台问题处理
服务器·音视频·技术分享
搬码临时工5 小时前
自己本地搭建的服务器怎么接公网?公网IP直连服务器方法,和只有内网IP直接映射到互联网
服务器·网络·tcp/ip
AI云原生6 小时前
2025最新国内服务器可用docker源仓库地址大全(2025年8月更新)
运维·服务器·docker·云原生·容器·kubernetes·serverless
火龙kess7 小时前
Centos-Stream 10 安装教程(2025版图文教程)
linux·运维·centos
菜鸟也会Fly9 小时前
【/usr/bin/env: “bash\r”: 没有那个文件或目录】问题解决
linux·bash
楠目9 小时前
Shell脚本实现自动封禁恶意扫描IP
服务器·网络·tcp/ip
程序员黄老师10 小时前
Linux tail 命令详解
linux·运维