QDateTime::currentMSecsSinceEpoch() resolution is 10 ms, not 1ms



  • Hi,
    i have a problem with QDateTime::currentMSecsSinceEpoch(). A simple console application
    shows the problem.

    #include <QCoreApplication>
    #include <QDebug>
    #include <QDateTime>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        for( int i=0; i<1000; ++i )
        {
            qDebug() << Q_FUNC_INFO << QDateTime::currentMSecsSinceEpoch();
        }
        return a.exec();
    }
    

    The timer jumps every 10 ms and it was definitely not the case some weeks ago.
    I'm using QDateTime::currentMSecsSinceEpoch() as timestamp for incoming UDP
    measurement packages over UDP. Some weeks ago the measurement timestamps
    were ok, but now they are jumping for 10 ms.

    Has someone an idea or similar problems?

    Regards
    Stefan

    0_1522217201150_QDateTimeMilliseconds.png



  • @MECoder said in QDateTime::currentMSecsSinceEpoch() resolution is 10 ms, not 1ms:

    for( int i=0; i<1000; ++i )
    {
    qDebug() << Q_FUNC_INFO << QDateTime::currentMSecsSinceEpoch();
    }

    can't reproduce that.

    this is my result, it increases each milisecond:
    0_1522217935483_3d4259e5-8b3d-4253-ad8b-0c30d17fd5da-image.png


  • Qt Champions 2017

    @MECoder

    • Which Qt version are you using?
    • Have you changed this Qt version since your last tests?


  • Hi,
    my post as pure text is always flagged as spam by akismet. So i put everything from preview into a picture below.

    0_1522221664093_Answer.png


  • Qt Champions 2017

    @MECoder

    What happens if you change QTime to QElapsedTimer?

    From my older experiences with Windows, I can confirm that the API had some minimal time between events. There was a call in mmsystem.dll to speed up this, but I don't remember the API call at the moment.

    Edit: the Windows API call for higher resolution was timeBeginPeriod(). I used it to get shorter sleep times than 10 milliseconds. Be aware of the side effects that may have.

    If possible, use QElaspsedTimer for time interval measurement.



  • @aha_1980 said in QDateTime::currentMSecsSinceEpoch() resolution is 10 ms, not 1ms:

    What happens if you change QTime to QElapsedTimer?

    Interesting!

    QElapsedTimer elatime;
    qDebug() << Q_FUNC_INFO << QDateTime::currentMSecsSinceEpoch() << time.elapsed() << elatime.elapsed();
    

    i get this result. QElapsedTimer increases in milliseconds while QDateTime and QTime in steps of 10ms.

    int main(int, char**) 1522222320473 40 31
    int main(int, char**) 1522222320473 40 32
    int main(int, char**) 1522222320473 40 33
    int main(int, char**) 1522222320473 40 34
    int main(int, char**) 1522222320473 40 34
    int main(int, char**) 1522222320473 40 35
    int main(int, char**) 1522222320473 40 36
    int main(int, char**) 1522222320473 40 37
    int main(int, char**) 1522222320473 40 38
    int main(int, char**) 1522222320473 40 39
    int main(int, char**) 1522222320483 50 41
    int main(int, char**) 1522222320483 50 43
    int main(int, char**) 1522222320483 50 46
    int main(int, char**) 1522222320483 50 48
    int main(int, char**) 1522222320483 50 49
    int main(int, char**) 1522222320493 60 51
    int main(int, char**) 1522222320493 60 53
    int main(int, char**) 1522222320493 60 54
    int main(int, char**) 1522222320493 60 55
    int main(int, char**) 1522222320493 60 57
    int main(int, char**) 1522222320493 60 59
    

  • Qt Champions 2017

    @MECoder said in QDateTime::currentMSecsSinceEpoch() resolution is 10 ms, not 1ms:

    QElapsedTimer increases in milliseconds while QDateTime and QTime in steps of 10ms.

    That is because QElapsedTimer don't use the API QTime uses.

    Please see my edit in my last post also.



  • @aha_1980 said in QDateTime::currentMSecsSinceEpoch() resolution is 10 ms, not 1ms:

    That is because QElapsedTimer don't use the API QTime uses.
    Please see my edit in my last post also.

    Thx! I have changed my software to a fixed starting timestamp from QDateTime::currentMSecsSinceEpoch()
    and add the value from QElapsedTimer::elapsed() each event when i receive the UDP package with measurement data.
    This solved my problem. Great!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.