[笔记]c++基础实践《四》chrono库使用

简介: [笔记]c++基础实践《四》chrono库使用

前言

常用

睡眠

#include <chrono>
#include <thread>
void main(){
  std::this_thread::sleep_for(std::chrono:: milliseconds (1000)); //休眠1000毫秒
}

获得当前程序运行毫秒

#include <ctime>
void main(){
  int tick=(int)(std::clock()*1000/ CLOCKS_PER_SEC);

计时

方法1

#include <chrono>
#include <thread>
void main(){
    auto start = std::chrono::high_resolution_clock::now();
  std::this_thread::sleep_for(std::chrono:: milliseconds (2000));
  auto end = std::chrono::high_resolution_clock::now();
  uint64_t time  = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    printf("time:%ld\n",time);
}

方法2

#include <chrono>
#include <thread>
void main(){
    auto start = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  std::this_thread::sleep_for(std::chrono:: milliseconds (2000));
  uint64_t cur_time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  uint64_t spend_time  = cur_time - start ;
    printf("time:%ld\n", spend_time);
}

总结


相关文章
|
14天前
|
算法 C++
算法笔记:递归(c++实现)
算法笔记:递归(c++实现)
|
13天前
|
编译器 C++
《Effective C++ 改善程序与设计的55个具体做法》 第一章 笔记
《Effective C++ 改善程序与设计的55个具体做法》 第一章 笔记
|
15天前
|
存储 自然语言处理 安全
C++ STL标准库 《string原理与实战分析》
C++ STL标准库 《string原理与实战分析》
19 0
|
7天前
|
存储 算法 C++
C++一分钟之-标准模板库(STL)简介
【6月更文挑战第21天】C++ STL是高效通用的算法和数据结构集,简化编程任务。核心包括容器(如vector、list)、迭代器、算法(如sort、find)和适配器。常见问题涉及内存泄漏、迭代器失效、效率和算法误用。通过示例展示了如何排序、遍历和查找元素。掌握STL能提升效率,学习过程需注意常见陷阱。
22 4
|
5天前
|
关系型数据库 MySQL 测试技术
技术分享:深入C++时间操作函数的应用与实践
技术分享:深入C++时间操作函数的应用与实践
11 1
|
10天前
|
C++
C++解决线性代数矩阵转置 小实践
【6月更文挑战第3天】C++解决线性代数矩阵转置
16 2
|
1天前
|
域名解析 网络协议 程序员
程序员必知:【转】adns解析库——域名解析实例(C++、linux)
程序员必知:【转】adns解析库——域名解析实例(C++、linux)
10 0
|
1天前
|
C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
7 0
|
1天前
|
存储 C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
7 0
|
1天前
|
缓存 C++
详细解读C++常用库函数C函数库cstdio
详细解读C++常用库函数C函数库cstdio