游戏服务器对游戏性能,游戏服务器之性能统计

这是一个C++实现的毫秒级定时器类,用于精确到毫秒的时间间隔判断。类包含多个构造函数以满足不同初始化需求,提供重置、延时和检查定时器是否到达指定时间的功能,并能获取倒计时的剩余秒数和毫秒数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**

* \author cjy

* \description 定时器

* 固定时间间隔的定时器,方便对于时间间隔的判断,精确到毫秒级

*/

class timer

public:

/**

* \author cjy

* \description构造函数

* \param how_long 定时器的时间,单位:毫秒

* \param first 有些定时器可能希望在启动时就可以执行一次,所以不能直接addDelay哦

*/

explicit timer(const int64 how_long, bool first=false, const int64 delay=0) : _long(how_long), _timer()

if(!first)

_timer.addDelay(_long+delay);

/**

* \author cjy

* \description构造函数

* \param how_long 定时器的时间,单位:毫秒

* \param first 有些定时器可能希望在启动时就可以执行一次,所以不能直接addDelay哦

* \param ctv 当前时间

*/

explicit timer(const int64 how_long, bool first , realtime &ctv) : _long(how_long), _timer(ctv)

if(!first)

_timer.addDelay(_long);

/**

* \author cjy

* \description重新设置定时器的精度和开始计时时间

* \param how_long 定时器的时间,单位:毫秒

* \param ctv 当前时间

*/

void reset(const uint64 how_long, const realtime &cur)

_long = how_long;

_timer = cur;

_timer.addDelay(_long);

/**

* \author cjy

* \description重新设置定时器的时间

* \param cur 指定定时器启动的时间

*/

void current(const realtime &cur)

_timer = cur;

/**

* \author cjy

* \description延时定时器时间

* \param cur 指定定时器启动的时间

* \param delay 延时时间

*/

void next(const realtime &cur, const uint32 delay)

_timer = cur;

_timer.addDelay(delay);

/**

* \author cjy

* \description重新设置定时器的时间

* \param cur 指定定时器启动的时间

*/

void next(const realtime &cur)

_timer = cur;

_timer.addDelay(_long);

/**

* \author cjy

* \description倒计时剩余秒数.不受时间调整影响.

* \param cur 当前时间

* return 剩余描述

*/

inline uint32 leftSec(const realtime &cur)

return (_timer.sec() > cur.sec()) ? (_timer.sec() - cur.sec()) : 0;

/**

* \author cjy

* \description倒计时剩余毫秒数.受时间调整影响

* \param cur 当前时间

* return 剩余值

*/

inline uint64 leftMSec(const realtime &cur)

return (_timer._msec > cur._msec) ? (_timer._msec - cur._msec) : 0;

/**

* \author cjy

* \description定时器检查

* \param cur 检查定时器的时间

* return 是否到达指定时间

*/

inline bool operator() (const realtime &cur)

if (_timer._msec > cur._msec)

return false;

addDelay(cur);

return true;

inline void addDelay(uint64 addLong)//添加延迟

_timer.addDelay(addLong);

private:

/**

* \author cjy

* \description定时器时间间隔

*/

uint64 _long;

/**

* \author cjy

* \description上次检查定时器的时间

*/

realtime _timer;

private:

/**

* \author cjy

* \description非严格检测,存在积累误差

* 从效率方面考虑,严格监测定时器的需求并不是必须的,去除对严格监测的支持

*/

inline void addDelay(const realtime& cur)

_timer = cur;

_timer.addDelay(_long);

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值