QT6 源(89):阅读与注释计时器类 QElapsedTimer ,源代码以及属性测试。该类没有继承于 QObject

(1)本简单示例综合运用了该类的成员函数

在这里插入图片描述

(2)本源代码来自于头文件 qelapsedtimer . h

#ifndef QELAPSEDTIMER_H
#define QELAPSEDTIMER_H

#include <QtCore/qglobal.h>

QT_BEGIN_NAMESPACE //可见,本类定义于 Qt 的命名空间

/*
The QElapsedTimer class provides a fast way to calculate elapsed times.

QElapsedTimer类通常用于快速计算两个事件之间已经过去了多长时间。
它的 API 与QTime 类似,因此使用该类的代码可以快速移植到新类。
然而,与 QTime不同,QElapsedTimer 尝试在可能的情况下使用单调时钟。
这意味着无法将QElapsedTimer 对象转换为人类可读的时间。

如下面的示例所示:
该类典型的用例是确定在慢操作中花费了多少时间。这种用例的最简单例子是用于调试目的,

     QElapsedTimer timer;
     timer.start();

     slowOperation1();

     qDebug() << "The slow operation took" << timer.elapsed() << "milliseconds";

经过的时间也可以用于重新计算完成第一个操作后可用于另一个操作的时间。
当执行必须在一定时间内完成,但需要几个步骤时,这很有用。
QI0Device及其子类中的waitFor类型函数就是这种需要的良好示例。在这种情况下,代码可能如下所示:

    void executeSlowOperations(int timeout)
    {
        QElapsedTimer timer;
        timer.start();
        slowOperation1();

        int remainingTime = timeout - timer.elapsed();

        if (remainingTime > 0)   slowOperation2(remainingTime);
    }

另一个用例是为特定的时间段执行某些操作。为此,QElapsedTimer提供了hasExpired()便捷函数,
可用于确定一定数量的毫秒是否已经流逝:

    void executeOperationsForTime(int ms)
    {
        QElapsedTimer timer;
        timer.start();

        while ( !timer.hasExpired(ms) )
            slowOperation1();
    }

在这种情况下,使用 QDeadlineTimer通常更方便,因为它计算的是未来的超时时间,而不是跟踪已用时间。

Reference Clocks:
在所有支持的平台中,QElapsedTimer将使用平台的单调参考时钟(参见QElapsedTimer:isMonotonic())。
这还有一个额外的好处,即QElapsedTimer不受时间调整的影响,例如用户更正时间。
此外,与QTime不同,QElapsedTimer不受时区设置变化的影响,例如夏令时。
另一方面,这意味着 QElapsedTimer值只能与其他使用相同引用的值进行比较。
如果时间是从QElapsedTimer对象(QElapsedTimer::msecsSinceReference())中提取的,
    并且进行了序列化,则尤其如此。这些值不应该通过网络交换或保存到磁盘,
    因为无法确定接收数据的计算机节点是否与发送数据的计算机节点相同,或者它是否已经重新启动。
然而,如果其他在相同机器上运行的进程也使用相同的参考时钟,则可以与它们交换值。
QElapsedTimer将始终使用相同的时钟,因此可以安全地与同一机器上其他进程产生的值进行比较。
如果比较的是其他API产生的值,
则应检查使用的时钟是否与OElapsedTimer相同(请参阅QElapsedTimer::clockType()).

32-bit overflows :
QElapsedTimer使用的一些时钟具有有限的范围,并且在达到上限(通常为32位)后可能会溢出。
QElapsedTimer处理此溢出问题并提供一致的计时。
但是,当从QElapsedTimer提取自参考以来的时间时,
    同一机器中的两个不同进程可能对实际流逝了多少时间有不同的理解。
关于哪些时钟类型可能会溢出以及如何解决该问题的信息是与时钟类型一起记录的。

*/

class Q_CORE_EXPORT QElapsedTimer
{
private:
    qint64 t1;
    qint64 t2;

public:
    //此枚举包含 QElapsedTimer 可能使用的不同时钟类型。  //单调 Monotonic
    //QElapsedTimer在特定机器上始终使用相同的时钟类型,因此在程序生命周期内该值不会变化。
    //它被提供,以便QElapsedTimer可以与其他非Ot实现一起使用,以确保使用相同的参考时钟。
    enum ClockType {
        SystemTime       , //人类可读的系统时间。这个时钟不是单调的。
        MonotonicClock   , //系统的单调时钟,通常出现在Unix系统中。这个时钟是单调的,不会溢出。
        TickCounter      , //Windows系统中使用的系统计秒器。这个时钟可能会溢出。
        MachAbsoluteTime , //Mach内核的绝对时间(macOs和i0S)。这个时钟是单调的,不会溢出。
        PerformanceCounter //Windows提供的高分辨率性能计数器。这个时钟是单调的,不会溢出。
    };

    //Constructs an invalid QElapsedTimer.
    //A timer becomes valid once it has been started.
    constexpr QElapsedTimer()
        : t1(Q_INT64_C(0x8000000000000000)), t2(Q_INT64_C(0x8000000000000000)) { }

    //Starts this timer. Once started, a timer value can be checked with
    //elapsed() or msecsSinceReference().
    //Normally, a timer is started just before a lengthy operation.
    //Also, starting a timer makes it valid again.
    void     start() noexcept;
    qint64 restart() noexcept; //返回本活动时钟韩国已经工作计时的 ms数。
    //重新启动计时器并返回自上次启动以来经过的毫秒数。此函数等效于使用elapsed()获取经过的时间,
    //然后使用 start()再次启动计时器,但它在一个操作中完成,避免了两次获取时钟值的需要.
    //在无效的 QElapsedTimer上调用此函数会导致未定义行为。

    //Returns the clock type that this QElapsedTimer implementation uses.
    static ClockType clockType  () noexcept; //这是个静态成员函数。

    static  bool     isMonotonic() noexcept;
    //Returns true if this is a monotonic clock, false otherwise.
    //See the information on the different clock types to
    //understand which ones are monotonic.

    //Returns false if the timer has never been started or
    //invalidated by a call to invalidate().
            bool     isValid    () const noexcept;

    void invalidate() noexcept; //使正在工作的时钟失效
    //Marks this QElapsedTimer object as invalid.
    //An invalid object can be checked with isValid().
    //Calculations of timer elapsed since invalid data are undefined and will
    //likely produce bizarre results. //产生奇怪的结果。
    
    //Returns the number of milliseconds since this QElapsedTimer was last started.
    //Calling this function on a QElapsedTimer that is invalid
    //  results in undefined behavior.
    qint64      elapsed() const noexcept;
    qint64 nsecsElapsed() const noexcept;
    //Returns the number of nanoseconds since this QElapsedTimer was last started.
    //On platforms that do not provide nanosecond resolution,
    //the value returned will be the best estimate available.
    
    bool hasExpired(qint64 timeout) const noexcept; //形参的单位是毫秒ms
    //Returns true if this QElapsedTimer has already expired
    //by timeout milliseconds (that is, more than timeout milliseconds have elapsed).
    //The value of timeout can be -1 to indicate that this timer does not expire,
    //in which case this function will always return false.
    
    //Returns the number of seconds between this QElapsedTimer and other.
    //If other was started before this object, the returned value will be negative.
    //If it was started later, the returned value will be positive.
    //Calling this function on or with a QElapsedTimer that is
    //invalid results in undefined behavior.
    qint64  secsTo(const QElapsedTimer & other) const noexcept;
    qint64 msecsTo(const QElapsedTimer & other) const noexcept;
    //Returns the number of milliseconds between this QElapsedTimer and other. 
    qint64 msecsSinceReference() const noexcept;
    //返回此 QElapsedTimer对象上次启动与其参考时钟的启动之间的时间间隔(以毫秒为单位)。
    //对于除QElapsedTimer::SystemTime时钟以外的所有时钟,该数字通常是任意的。
    //对于该时钟类型该数字是1970年1月1日0:00 UTC(即,以毫秒表示的Unix时间)以来的毫秒数。
    //在Linux、Windows和苹果平台上,这个值通常是系统启动以来的时间,
    //尽管它通常不包括系统处于睡眠状态的时间。
     
    friend bool operator==(const QElapsedTimer & lhs, const QElapsedTimer & rhs) noexcept
    { return lhs.t1 == rhs.t1 && lhs.t2 == rhs.t2; }

    friend bool operator!=(const QElapsedTimer & lhs, const QElapsedTimer & rhs) noexcept
    { return !(lhs == rhs); } //说明支持对本类型的 ==  !=  < 操作

    friend bool Q_CORE_EXPORT operator<(const QElapsedTimer & lhs,
                                        const QElapsedTimer & rhs) noexcept;

}; //完结 class QElapsedTimer

QT_END_NAMESPACE

#endif // QELAPSEDTIMER_H

(3)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值