void MyObject::progress( float current ){ int percents = qRound( current/total*100 ); if ( percents == oldPercents_ ) return; oldPercents_ = percents; progressBar_->setValue( percents );}
#include <iostream>#include <QObject>#include <windows.h>class Object1 : public QObject{ Q_OBJECTpublic: void emitSignals(size_t N) for(size_t i=0; i<N; i++) { emit TestSignal(); }signals: void TestSignal();};class Object2 : public QObject{ Q_OBJECTpublic slots: void SignalReceiver() {}};class PerformanceTimer{public:PerformanceTimer(): m_frequency(0), m_startTimeStamp(0), m_stopTimeStamp(0), m_totalTime(0), m_isHighResolution(false), m_resolution(0){ if(::QueryPerformanceFrequency((PLARGE_INTEGER)&m_frequency)) { m_isHighResolution = true; } else { m_isHighResolution = false; m_frequency = 1000; } m_resolution = static_cast<double>(1) / static_cast<double>(m_frequency);}void Start() // start timer{ m_startTimeStamp = m_stopTimeStamp = GetTimeStamp();}void Stop() // stop timer{ m_stopTimeStamp = GetTimeStamp(); m_totalTime += m_stopTimeStamp-m_startTimeStamp;}void ResetTotalTime() // set total time to 0{ m_totalTime = 0;}double Duration() const // last Start/Stop duration in seconds{ return static_cast<double>(m_stopTimeStamp - m_startTimeStamp) * m_resolution;}double TotalTime() const // sum of all Start/Stop durations in seconds{ return static_cast<double>(m_totalTime) * m_resolution;}bool IsHighResolution() const{ return m_isHighResolution;}__int64 Frequency() const{ return m_frequency;}double Resolution() const{ return m_resolution;}private: __int64 m_frequency; __int64 m_startTimeStamp; __int64 m_stopTimeStamp; __int64 m_totalTime; bool m_isHighResolution; double m_resolution;__int64 GetTimeStamp() const{ if(m_isHighResolution) { __int64 ticks = 0; ::QueryPerformanceCounter((PLARGE_INTEGER)&ticks); return ticks; } return ::GetTickCount();} };int main(){ Object1 obj1; Object2 obj2; QObject::connect(&obj1, SIGNAL(TestSignal()), &obj2, SLOT(SignalReceiver())); PerformanceTimer timer; const size_t N = 10000000; timer.Start(); obj1.emitSignals(N); timer.Stop(); std::cout << "Emited signals count: " << N << std::endl; std::cout << "Total time: " << timer.Duration() << std::endl; std::cout << "Signals per second: " << (size_t)((double)N/timer.Duration()) << std::endl; return 0;}
Emited signals count: 10000000Total time: 6.17696Signals per second: 1618919
int main(){ Object1 obj1; Object2 obj2; Object2 obj3; QObject::connect(&obj1, SIGNAL(TestSignal()), &obj2, SLOT(SignalReceiver())); QObject::connect(&obj1, SIGNAL(TestSignal()), &obj3, SLOT(SignalReceiver())); PerformanceTimer timer; const size_t N = 10000000; timer.Start(); obj1.emitSignals(N); timer.Stop(); std::cout << "Emited signals count: " << N << std::endl; std::cout << "Total time: " << timer.Duration() << std::endl; std::cout << "Signals per second: " << (size_t)((double)N/timer.Duration()) << std::endl; return 0;}
Emited signals count: 10000000Total time: 8.20403Signals per second: 1218912
#ifndef OBJECT1_H#define OBJECT1_H#include <QObject>#include "object2.h"/** @author Alexander Novikov <nova@alba.ua>*/class Object1 : public QObject{Q_OBJECTpublic: Object1(QObject *parent=0); ~Object1(); void emitSignals(qint64 N); void callFunction(Object2* obj,qint64 N); void callFunction2(Object2* obj1,Object2* obj2,qint64 N); signals: void TestSignal();};#endif
#include "object1.h"Object1::Object1(QObject *parent) : QObject(parent){}Object1::~Object1(){}void Object1::emitSignals(qint64 N){ for(qint64 i=0; i<N; i++) { emit TestSignal(); }}void Object1::callFunction(Object2 * obj, qint64 N){ for(qint64 i=0; i<N; i++) { obj->SignalReceiver(); }}void Object1::callFunction2(Object2 * obj1, Object2 * obj2, qint64 N){ for(qint64 i=0; i<N; i++) { obj1->SignalReceiver(); obj2->SignalReceiver(); }}
#ifndef OBJECT2_H#define OBJECT2_H#include <QObject>/** @author Alexander Novikov <nova@alba.ua>*/class Object2 : public QObject{ Q_OBJECTpublic: Object2(QObject *parent=0); ~Object2(); public slots: void SignalReceiver();};#endif
#include "object2.h"Object2::Object2(QObject *parent) : QObject(parent){}Object2::~Object2(){}void Object2::SignalReceiver(){}
#ifndef TESTOBJECT_H#define TESTOBJECT_H#include <QObject>class Object1;class Object2;/** @author Alexander Novikov <nova@alba.ua>*/class TestObject : public QObject{Q_OBJECTpublic: TestObject(QObject *parent = 0); ~TestObject(); void runTest(qint64 N); private:Object1 *obj1;Object2 *obj2;};#endif
#include "testobject.h"#include "object1.h"#include "object2.h"TestObject::TestObject(QObject *parent) : QObject(parent){ obj1=new Object1(this); obj2=new Object2(this); connect( obj1, SIGNAL(TestSignal()), obj2, SLOT(SignalReceiver()));}TestObject::~TestObject(){}void TestObject::runTest(qint64 N){ obj1->emitSignals(N);}
#include <QtCore>#include <QDebug>#include "object1.h"#include "object2.h"#include "testobject.h"int main(int argc, char *argv[]){ QCoreApplication app(argc, argv); Object1 obj1; Object2 obj2; Object2 obj3; QObject::connect( &obj1, SIGNAL(TestSignal()), &obj2, SLOT(SignalReceiver())); QTime timer; double time; const qint64 N = 100000000; timer.start(); obj1.callFunction(&obj2,N); time=(double)timer.elapsed()/1000; qDebug() << "objectCall count:" << N; qDebug() << "objectCall total time: " << time; qDebug() << "Call per second: " << (size_t)((double)N/time); timer.restart(); obj1.callFunction2(&obj2,&obj3,N); time=(double)timer.elapsed()/1000; qDebug() << "objectCall2 count:" << N; qDebug() << "objectCall2 total time: " << time; qDebug() << "Call2 per second: " << (size_t)((double)N/time); timer.restart(); obj1.emitSignals(N); time=(double)timer.elapsed()/1000; qDebug() << "Emited signals count: " << N; qDebug() << "Total time: " << time; qDebug() << "Signals per second: " << (size_t)((double)N/time); QObject::connect( &obj1, SIGNAL(TestSignal()), &obj3, SLOT(SignalReceiver())); timer.restart(); obj1.emitSignals(N); time=(double)timer.elapsed()/1000; qDebug() << "On two slot connected"; qDebug() << "Emited signals count: " << N; qDebug() << "Total time: " << time; qDebug() << "Signals per second: " << (size_t)((double)N/time); TestObject to; timer.restart(); to.runTest(N); time=(double)timer.elapsed()/1000; qDebug() << "TestObject slot connected"; qDebug() << "Emited signals count: " << N; qDebug() << "Total time: " << time; qDebug() << "Signals per second: " << (size_t)((double)N/time); return 0;}
SOURCES += main.cpp \object1.cpp \object2.cpp \testobject.cppTEMPLATE = appCONFIG += warn_on \ qt \ releaseTARGET = testQT -= guiHEADERS += object1.h \object2.h \testobject.hQT += core
TEMPLATE = appCONFIG += consoleCONFIG += releaseCONFIG += warn_onTARGET = test DESTDIR = ./HEADERS += object1.h \ object2.h \ testobject.h SOURCES += main.cpp \ object1.cpp \ object2.cpp \ testobject.cpp
#ifndef OBJECT1_H#define OBJECT1_H#include <qobject.h>class Object2;class Object1 : public QObject{Q_OBJECTpublic: Object1(QObject *parent = 0, const char *name = 0); ~Object1(); void emitSignals(int N); void callFunction(Object2* obj,int N); signals: void TestSignal();};#endif
#include "object1.h"#include "object2.h"Object1::Object1(QObject *parent, const char *name) : QObject(parent, name){}Object1::~Object1(){}void Object1::emitSignals(int N){ for(int i=0; i<N; i++) { emit TestSignal(); }}void Object1::callFunction(Object2 * obj, int N){ for(int i=0; i<N; i++) { obj->SignalReceiver(); }}
#ifndef OBJECT2_H#define OBJECT2_H#include <qobject.h>class Object2 : public QObject{Q_OBJECTpublic: Object2(QObject *parent = 0, const char *name = 0); ~Object2(); public slots: void SignalReceiver();};#endif
#include "object2.h"Object2::Object2(QObject *parent, const char *name) : QObject(parent, name){}Object2::~Object2(){}void Object2::SignalReceiver(){}
#include <qdatetime.h>#include <qapplication.h>#include "object1.h"#include "object2.h"int main( int argc, char ** argv ) { QApplication a( argc, argv ); QTime timer; int N= 100000000; double time; Object1 obj1; Object2 obj2; Object2 obj3; QObject::connect(&obj1, SIGNAL(TestSignal()), &obj2, SLOT(SignalReceiver())); timer.start(); obj1.callFunction(&obj2,N); time=(double)timer.elapsed()/1000; qDebug("Call count: %d",N); qDebug("Total time: %f",time); qDebug("Call per second: %d\n",(size_t)((double)N/time)); timer.restart(); obj1.emitSignals(N); time=(double)timer.elapsed()/1000; qDebug("Emited count: %d",N); qDebug("Total time: %f",time); qDebug("Signals per second: %d\n",(size_t)((double)N/time)); qDebug("Connect obj3"); QObject::connect(&obj1, SIGNAL(TestSignal()), &obj2, SLOT(SignalReceiver())); timer.restart(); obj1.emitSignals(N); time=(double)timer.elapsed()/1000; qDebug("Emited count: %d",N); qDebug("Total time: %f",time); qDebug("Signals per second: %d\n",(size_t)((double)N/time)); return 0;}
SOURCES += main.cpp \ object1.cpp \ object2.cppTEMPLATE = appCONFIG += release \ warn_on \ thread \ qtTARGET = test3HEADERS += object1.h \object2.h