[Development] Performance of QUdpSocket writeDatagram between Qt4 and Qt5 in different GNU/Linux platforms

Mauro Brenna malloblenne at gmail.com
Tue Feb 25 16:56:53 CET 2014


Hello,

I am facing an unusual behaviour related to the performance of the method
writeDatagram of QUdpSocket using two different version of QT ( Qt4.8.5 vs
Qt5.1.1).

I tested the same program both on Ubuntu between 4.8.1 and 5.0 and the
results are fine.
While on an emDebian arm platform, Qt5.1.1 performs much worse than Qt4.8.5
(two order of magnitude times slower).

Basically, my application is just calling writeDatagram in a for loop and
returns the average time writeDatagram calls.
The results after 10000 calls, sending 19bytes of payload are:
 Qt 4.8.5 avg 0.000224s each call
 Qt 5.1.1 avg 0.033..s (about 30ms each message).

 I checked in Wireshark and I am able to receive the messages in both cases.
 Moreover, I tested vanilla c socket and they perform as good as qt 4.8.5.
 I wonder how it is possible, and if you have any suggestion in merit.
 I looked a bit in the qt code and saw a bind() inside the QT5
writeDatagram method, which I do not immediately understand but I do not
know if might cause the issue.
 Should I specify something more in the initialization of the QUdpSocket?
Are there any pauses, wait or blocking mechanisms?
 Might my ethernet interface configuration be wrong? How should I adjust it
in that case?

 Regards,


 Below is my code:

//Util.h
#pragma once
#ifdef __linux__
#include <time.h>
#else
#define NOMINMAX
#include <windows.h>
#include <time.h>
#endif

class Util
{
  public:
      inline static float GetTimeSeconds(void)
      {
       #ifdef __linux__
         static bool initialized = false;
         static struct timespec offsetTime;
         struct timespec startTime;
         clock_gettime( CLOCK_MONOTONIC, &startTime);
         if (!initialized)
         {
            offsetTime.tv_sec = startTime.tv_sec;
            offsetTime.tv_nsec = startTime.tv_nsec;
            initialized = true;
         }
         return static_cast<float>( (startTime.tv_sec - offsetTime.tv_sec)
 + (startTime.tv_nsec - offsetTime.tv_nsec)/1.0e9);
       #else
         return static_cast<float>(clock())/
static_cast<float>(CLOCKS_PER_SEC);
       #endif
      }
};

//Task.h
#pragma once
#include <QtCore>
#include <QtNetwork>
#include <QUdpSocket>
#include "Util.h"

class Task : public QObject
{
    Q_OBJECT
public:
    Task(QObject *parent = 0) : QObject(parent) {}

public slots:
    void run()
    {
        // Do processing here
        unsigned int messageNo = 0;
        QUdpSocket *udpSocket;
        udpSocket = new QUdpSocket(this);
        int times = 10000;
        QByteArray datagram = "Broadcast message " +
QByteArray::number(messageNo);
        const double t1 = Util::GetTimeSeconds();
      for (int i = 0; i<times; i++)
        {
           udpSocket->writeDatagram(datagram.data(), datagram.size(),
QHostAddress::Broadcast, 45454);
        }
        const double t2 = Util::GetTimeSeconds();
        qDebug(" writeDatagram(): %f s, total %f", (t2 - t1)/times, (t2 -
t1));//dt
        ++messageNo;
        emit finished();
        delete udpSocket;
    }

signals:
    void finished();
};

// main.cpp
#include "Task.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Task *task = new Task(&a);
    QObject::connect(task, SIGNAL(finished()), &a, SLOT(quit()));
    QTimer::singleShot(0, task, SLOT(run()));
    return a.exec();
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20140225/79331e8b/attachment.html>


More information about the Development mailing list