Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Qt Qt Creator gprof example 1: profiling a simple console application

 

This Qt example shows how to use gprof under Qt Creator to profile a simple console application.

 

 

Operating system: Ubuntu

IDE: Qt Creator 2.0.0

Project type: Qt4 GUI Application

Compiler: G++ 4.4.1

Libraries used:

 

 

 

 

 

Using gprof in Qt Creator 2.0.0

 

Here follows a step-by-step guide to use gprof to do the profiling of a simple Qt Creator project:

 

  1. Go to 'Projects -> Build Settings' and add a custom Build step. Fill in the information as in the screenshot below
  2. View screenshot
  3. View your project folder. There will be few files
  4. View screenshot
  5. Run the program. The executable 'profile_main' has been created in your project folder
  6. View screenshot
  7. Run 'profile_main' and the file 'gmon.out' is created
  8. View screenshot
  9. Start a Terminal, go to the project folder and use the command 'gprof profile_main > profile.txt'
  10. View screenshot
  11. The file 'profile.txt' will be created
  12. View screenshot
  13. The file 'profile.txt' will contain the profiling information

 

 

 

 

 

Qt project file

 

#-------------------------------------------------
#
# Project created by QtCreator 2010-07-24T13:44:59
#
#-------------------------------------------------
QT += core
QT -= gui
TARGET = CppGprofExample1
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp

 

 

 

 

 

Source code

 

#include <algorithm>
#include <cassert>
#include <vector>
#include <boost/foreach.hpp>

//From http://www.richelbilderbeek.nl/CppBubbleSort.htm
template <class T>
void BubbleSort(std::vector<T>& v)
{
  const int size = v.size();
  for(int i=0; i!=size-1; ++i)
  {
    for(int j=0; j!=size-i-1; ++j)
    {
      if(v[j] > v[j+1])
      {
        std::swap(v[j],v[j+1]);
      }
    }
  }
}

//From http://www.richelbilderbeek.nl/CppInsertionSort.htm
template <typename T>
void InsertionSort(std::vector<T>& v)
{
  const int size = v.size();
  for(int i=1; i!=size; ++i)
  {
    for(int j=0; j!=i; ++j)
    {
      if (v[j]>v[i])
      {
        const int temp=v[j];
        v[j]=v[i];
        for(int k=i;k>j;--k) { v[k]=v[k-1]; }
        v[j+1]=temp;
      }
    }
  }
}

//From http://www.richelbilderbeek.nl/CppSelectionSort.htm
template <typename T>
void SelectionSort(std::vector<T>& v)
{
  const int size = v.size();
  for(int i=0; i!=size-1; ++i)
  {
    for(int j=i+1; j!=size; ++j)
    {
      if (v[i]> v[j])
      {
        std::swap(v[i],v[j]);
      }
    }
  }
}

//From http://www.richelbilderbeek.nl/CppSortVector.htm
template <class T>
void SortVector(std::vector<T>& v)
{
  std::sort(v.begin(), v.end());
}

const std::vector<int> CreateShuffledVector(const std::size_t sz)
{
  std::vector<int> v(sz);

  int value = 0;
  BOOST_FOREACH(int i,v)
  {
    i = value;
    ++value;
  }
  std::random_shuffle(v.begin(),v.end());
  return v;
}


int main()
{
  const std::vector<int> v = CreateShuffledVector(10000);

  std::vector<int> v1(v);
  std::vector<int> v2(v);
  std::vector<int> v3(v);
  std::vector<int> v4(v);

  BubbleSort(v1);
  InsertionSort(v2);
  SelectionSort(v3);
  SortVector(v4);

  assert(v1==v2);
  assert(v2==v3);
  assert(v3==v4);
}

 

 

 

 

 

Profiling results

 

Here I show the results comparing the five functions, copied from the results file:

 

Flat profile:

  %   cumulative   self              self     total
time   seconds   seconds    calls  ms/call  ms/call  name
13.59      1.54     0.28        1   280.00   700.00  void BubbleSort<int>(std::vector<int, std::allocator<int> >&)
12.14      1.79     0.25        1   250.00   670.00  void InsertionSort<int>(std::vector<int, std::allocator<int> >&)
11.65      2.03     0.24        1   240.00   660.00  void SelectionSort<int>(std::vector<int, std::allocator<int> >&)
  0.00      2.06     0.00        1     0.00    24.98  void SortVector<int>(std::vector<int, std::allocator<int> >&)
  0.00      2.06     0.00        1     0.00     5.02  CreateShuffledVector(unsigned int)

 

Conlusion: as expected, SortVector (a QuickSort) is by far the quickest sorting algorithm.

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict