(C++) GprofQtCreatorExample2

January 11, 2018 · View on GitHub

 

 

 

 

 

(C++) GprofQtCreatorExample2

 

STLQt
CreatorLubuntu

 

gprof example 2: profiling a simple console application in Qt Creator, using Qt project file is a gprof example to profile a simple console application under Qt Creator.

 

 

To be able to use gprof to profile a project, add the following lines to a Qt project file:

 


QMAKE_CXXFLAGS_DEBUG += -pg QMAKE_LFLAGS_DEBUG += -pg

 

then, for a terminal, type the following lines:

 


qmake -config debug make clean make ./CppGprofQtCreatorExample2 gprof CppGprofQtCreatorExample2 > gprof.txt

 

Or easier, run the script profile.sh:

 


./profile.sh

 

 

 

 

 

Profiling results

 

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

 


Each sample counts as 0.01 seconds.   %   cumulative   self              self     total           time   seconds   seconds    calls   s/call   s/call  name     12.51    172.28    29.52        1    29.52    77.11  void BubbleSort<int>(std::vector<int, std::allocator<int> >&) 12.39    201.51    29.23        1    29.23    76.82  void InsertionSort<int>(std::vector<int, std::allocator<int> >&) 12.06    229.97    28.45        1    28.45    76.04  void SelectionSort<int>(std::vector<int, std::allocator<int> >&)   0.00    235.89     0.00        1     0.00     0.52  void SortVector<int>(std::vector<int, std::allocator<int> >&)   0.00    235.89     0.00        1     0.00     0.09  CreateShuffledVector(unsigned int)

 

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

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • STL STL: GNU ISO C++ Library, version 4.9.2

 

 

 

 

 

Qt project file: ./CppGprofQtCreatorExample2/CppGprofQtCreatorExample2.pro

 


include(../../ConsoleApplication.pri) #Or use the code below # QT += core # QT += gui # greaterThan(QT_MAJOR_VERSION, 4): QT += widgets # CONFIG   += console # CONFIG   -= app_bundle # TEMPLATE = app # CONFIG(release, debug|release) { #   DEFINES += NDEBUG NTRACE_BILDERBIKKEL # } # QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ # unix { #   QMAKE_CXXFLAGS += -Werror # } include(../../Libraries/Boost.pri) #Or use the code below # win32 { #   INCLUDEPATH += \ #     ../../Libraries/boost_1_54_0 # } SOURCES += main.cpp

 

 

 

 

 

./CppGprofQtCreatorExample2/main.cpp

 


#include <algorithm> #include <cassert> #include <iostream> #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(100000);   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);   #ifndef NDEBUG   std::cout << "Finished debug mode" << std::endl;   #else   std::cout << "Finished release mode" << std::endl;   #endif }

 

 

 

 

 

./CppGprofQtCreatorExample2/profile.sh

 


#!/bin/sh echo "Removing user file" rm *.pro.user #echo "Creating profile executable (debug mode)" #qmake -config debug echo "Creating profile executable (release mode)" qmake -config release make clean make echo "Removing makefile" rm Makefile echo "Removing object files" rm *.o echo "Start the application" ./CppGprofQtCreatorExample2 echo "Analyse the gprof results" gprof CppGprofQtCreatorExample2 > gprof.txt echo "Remove temporary gprof file" rm gmon.out