Profiling and Performance Tuning…

Problem: How to profile functions in my Qt app without using gprof or spending big bucks and buying profiling tools for VS2005?

Solution: Have the following contents in a header file, Profiler.h for example.

#include <QTime>

class FunctionProfiler
FunctionProfiler(const QString& func) {
m_function = QMetaObject::normalizedSignature( qPrintable(func) );
m_function = m_function.remove("__thiscall");
m_function = m_function.remove("__stdcall");
m_function = m_function.remove("__cdecl");

m_startTime = QTime::currentTime();

~FunctionProfiler() {
qint32 msecs = m_startTime.msecsTo( QTime::currentTime() );
qDebug("PROFILE: (%5d) %s", msecs, qPrintable(m_function));

QString m_function;
QTime m_startTime;

#define PROFILE_THIS_FUNCTION FunctionProfiler functionProfiler(Q_FUNC_INFO);

Insert PROFILE_THIS_FUNCTION line into the first line of each function you want to profile. Compile and execute the program
by redirecting (2>) debug output to a text file. Then look for those PROFILE lines to get a approximate feel of time spent
by your functions.

This trick got me through some tough challenges in my current project. It is a no-brainer, but quite useful.