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);
相关推荐
tcwgq26 分钟前
Centos Stream 8 搭建Cas Server
linux·elasticsearch·centos
鸽芷咕1 小时前
Rokid 手势识别技术深度解析:解锁 AR 无接触交互的核心秘密
linux
一枚正在学习的小白2 小时前
PG数据文件位置迁移
linux·运维·服务器·数据库
Tfly__2 小时前
Ubuntu 20.04 安装Aerial Gym Simulator - 基于 Gym 的无人机强化学习仿真器
linux·人工智能·ubuntu·github·无人机·强化学习·运动规划
博睿谷IT99_2 小时前
Linux 云计算核心技术:原理、组件与 K8s 实战部署
linux·kubernetes·云计算
疯癫的老码农2 小时前
【Linux环境下安装】SpringBoot应用环境安装(二)-Redis安装
linux·spring boot·redis
点亮一颗LED(从入门到放弃)3 小时前
SQLite3数据库——Linux应用
linux·数据库·sqlite
我科绝伦(Huanhuan Zhou)3 小时前
Systemctl 与 Systemd 全面指南:Linux 系统服务管理详解
linux·服务器·网络
倔强的石头1063 小时前
【Linux指南】Linux命令行进度条实现原理解析
android·linux