嵌入式调试中,精确延时的实现模板

#ifndef TIME_UTIL_H
#define TIME_UTIL_H
#include<chrono>
#include<cstdint>
#include<iostream>
using namespace std;
#define TIMER_START(_X) static auto _X##_start = std::chrono::steady_clock::now(), _X##_stop = _X##_start; static int _X##times = 0;
#define TIMER_STOP(_X) _X##_stop = std::chrono::steady_clock::now(); ++(_X##times);
#define TIMER_NSEC(_X, _R) if(_X##times >= (_R)) {cout << std::chrono::duration_cast<std::chrono::nanoseconds >(_X##_stop - _X##_start).count() << " ns."  << endl;}
#define TIMER_USEC(_X, _R) if(_X##times >= (_R)) {cout << std::chrono::duration_cast<std::chrono::microseconds>(_X##_stop - _X##_start).count() << " us."  << endl;}
#define TIMER_MSEC(_X, _R) if(_X##times >= (_R)) {cout << std::chrono::duration_cast<std::chrono::milliseconds>(_X##_stop - _X##_start).count() << " ms."  << endl;}
#define TIMER_SEC(_X, _R)  if(_X##times >= (_R)) {cout << std::chrono::duration_cast<std::chrono::seconds     >(_X##_stop - _X##_start).count() << " s."   << endl;}
#define TIMER_MIN(_X, _R)  if(_X##times >= (_R)) {cout << std::chrono::duration_cast<std::chrono::minutes     >(_X##_stop - _X##_start).count() << " min." << endl;}
#endif

example:
TIMER_START(work)
work();
TIMER_STOP(work)
TIMER_USEC(work, 1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值