当前位置: 首页 > news >正文

c++:timer

1.设置休眠时间sleep_for

添加头文件

#include <thread>

#include <iostream>
#include <chrono>
#include <thread>int main(int argc, char const *argv[])
{// 休眠2秒std::this_thread::sleep_for(std::chrono::seconds(2));// 休眠500毫秒std::this_thread::sleep_for(std::chrono::milliseconds(500));// 休眠1000微妙std::this_thread::sleep_for(std::chrono::microseconds(1000));std::cout << "程序继续执行" << std::endl;return 0;
}

2.打印时间

添加头文件

#include <thread>

#include <chrono>
#Include <iomanip>  用于打印输出时间

2.1打印时间段(两个时间点的差值)

std::chrono::high_resolution_clock常用于获取精确时间的差值

    auto start = std::chrono::high_resolution_clock::now();std::this_thread::sleep_for(std::chrono::seconds(2));auto stop = std::chrono::high_resolution_clock::now();std::chrono::duration<double> duration = stop - start;std::cout << "stop-start=" << duration.count() << std::endl;

2.2输出当前系统时间

要输出当前系统时间一般用std::chrono::system::clock

    // 获取当前系统时间点auto now = std::chrono::system_clock::now();// 转换为time_t类型std::time_t now_c = std::chrono::system_clock::to_time_t(now);// 将time_t转换为本地时间std::tm *local_time = std::localtime(&now_c);// 格式化输出日期和时间std::cout << "当前系统时间是: " << std::put_time(local_time, "%Y-%m-%d %H:%M:%S") << std::endl;

2.3计时器

例:计算打印100次hello用了多少时间

#include <iostream>
#include <chrono>class Timer
{
public:std::chrono::time_point<std::chrono::high_resolution_clock> start, end;Timer(){start = std::chrono::high_resolution_clock::now();}~Timer(){end = std::chrono::high_resolution_clock::now();std::chrono::duration<float> duration = end - start;float ms = duration.count() * 1000.0f;std::cout << "Timer took " << ms << "ms" << std::endl;}
};
void function()
{Timer timer;for (int i = 0; i < 100; i++){std::cout << "hello\n";}
}int main(int argc, char const *argv[])
{function();return 0;
}

2.4使用c++制作简易定时器

#include <iostream>
#include <chrono>
#include <iomanip>
#include <string>class Timer
{
public:void starttime(){std::cout << "开始计时" << std::endl;start = std::chrono::high_resolution_clock::now();}void pausetime(){stop = std::chrono::high_resolution_clock::now();std::chrono::duration<double> duration = stop - start;std::cout << "计时结束" << std::endl;std::cout << "stop-start=" << duration.count() << std::endl;}void showtime(){auto now = std::chrono::system_clock::now();std::time_t now_c = std::chrono::system_clock::to_time_t(now);std::tm *local_time = std::localtime(&now_c);std::cout << "当前系统时间是:" << std::put_time(local_time, "%Y-%m-%d %H:%M:%S") << std::endl;}private:std::chrono::time_point<std::chrono::high_resolution_clock> start;std::chrono::time_point<std::chrono::high_resolution_clock> stop;
};int main(int argc, char const *argv[])
{Timer timer;char p;while (1){std::cout << "请输入字符 s:start p:pause t:showtime q:quit" << std::endl;std::cin >> p;switch (p){case 's':timer.starttime();break;case 'p':timer.pausetime();break;case 't':timer.showtime();break;case 'q':std::cout << "程序结束" << std::endl;return 0;}}return 0;
}

http://www.lryc.cn/news/499642.html

相关文章:

  • VSCode(四)CMake调试
  • 安装Docker并使用WSL
  • HCIA-openGauss_2_2连接与认证
  • 安装 pytorch lighting
  • 2024年12月7日历史上的今天大事件早读
  • ORB-SLAM2 ---- 非线性优化在SLAM中的应用(一)
  • FastAPI中创建一个多App架构
  • 计算机网络原理之HTTP与HTTPS
  • 完全按照手册win10里装Ubuntu 虚拟机然后编译ESP32(主要是想针对ESP32C3和S3)开发板的鸿蒙系统(失败)
  • vsphere vcenter web 界面的介绍
  • 【pyspark学习从入门到精通23】机器学习库_6
  • FPGA实战篇(呼吸灯实验)
  • 面经自测——自我介绍
  • 在 LS-DYNA 中将应力转换为用户定义的坐标系
  • 【Spark】 groupByKey与reduceByKey的区别
  • 数据库与数据库管理系统概述
  • (简单5步实现,免费且比GPT4.0更好用)部署本地AI大语言模型聊天系统:Chatbox AI + 马斯克grok2.0大模型
  • 滚珠螺杆导程的定义与重要性
  • 【特殊子序列 DP】力扣509. 斐波那契数
  • linux 架构详解
  • Spring Data Elasticsearch
  • OpenGL编译用户着色器shader
  • 过期策略、内存淘汰机制
  • Scala的正则表达式
  • 关于睡懒觉
  • 【算法day10】栈与队列:拓展与应用
  • 爆肝Android JNI - 延展Android蓝牙JNI学习
  • 总篇:Python3+Request+Pytest+Allure+Jenkins接口自动化框架设计思路
  • Java的Map介绍以及常见方法和三种遍历方式
  • C/C++基础知识复习(39)