Tag Archives: Programming

How to profile performance by hand

I recently needed to profile some C++ code that was taking longer than expected to run. The code was running on a machine without a profiler, so I wrote a handy Timer class that dumps nested timings of each method. You can initialize it to write either to a file or to std::cout if the machine has a console.

I originally thought of this as a ‘Poor Man’s Profiler’, but having used it there are real benefits to taking the trouble to instrument your own code – you can use the file dumps to swiftly compare performance between code changes; you can print out performance statistics and take along to meetings.

#include <iostream>
#include <chrono>
class TimerOutput
{
  public:
    TimerOutput( const std::string file_path = "" ) :
      file_path_( file_path )
    {
      if ( !file_path_.empty() )
      {
        file_.open( file_path_ );
      }
    }

    std::ostream& Stream()
    {
      if (file_path_.empty() )
        return std::cout;
      else
        return file_;
    }

private:
    std::string file_path_;
    std::ofstream file_;
};

class Timer
{
public:
    Timer( const std::string& description ) :
      description_(description),
      start_(std::chrono::system_clock::now())
    {
        applyIndent();
        timer_output_->Stream() << "Start " << description_.c_str() << "\n";
        ++indent;
    }

    ~Timer()
    {
        --indent;
        const std::chrono::time_point<std::chrono::system_clock> finish = std::chrono::system_clock::now();
        auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(finish - start_).count();

        applyIndent();
        timer_output_->Stream() << "Finished " << description_.c_str()
            << ", took " << milliseconds << " (ms)" << "\n";
}

private:
    void applyIndent() const
    {
        for ( size_t i = 0; i < indent; ++i )
        {
            timer_output_->Stream() << "--";
        }
    }

    static size_t indent;
    static TimerOutput* timer_output_;
    std::string description_;
    const std::chrono::time_point<std::chrono::system_clock> start_;
};

#define INITIALIZE_PROFILING_TO_CONSOLE() \
  TimerOutput timer_output; \
  size_t Timer::indent = 0; \
  TimerOutput* Timer::timer_output_ = &timer_output;

#define INITIALIZE_PROFILING_TO_FILE( path ) \
  TimerOutput timer_output( path ); \
  size_t Timer::indent = 0; \
  TimerOutput* Timer::timer_output_ = &timer_output;

#define TIME( description, f ) \
  { \
    Timer profiler( description ); \
    f; \
  }

The obvious limitation of my solution is that it uses a class static to achieve the levels of nesting, so it won’t work on multi-threaded code – but it was great for my purposes. Here’s some sample code that shows it in action:

#include "stdafx.h"

#include <thread>
#include <fstream>

#include "..\MusingStudio\Profiler.h"

//INITIALIZE_PROFILING_TO_CONSOLE()
INITIALIZE_PROFILING_TO_FILE( "c:/temp/timings.txt" )

void method2()
{
  TIME( "method2",
    std::chrono::milliseconds short_wait( 5 );
    std::this_thread::sleep_for( short_wait );
  )
}

void method1()
{
 TIME( "method1",
  TIME( "loop",
    for ( int i = 0; i &lt; 5; ++i )
    {
        method2();
    }
  )

  TIME( "expensive algorithm",
    std::chrono::milliseconds wait( 100 );
    std::this_thread::sleep_for( wait );
  )
 )
}

void method3()
{
  TIME( "method3",
    std::chrono::milliseconds long_wait( 500 );
    std::this_thread::sleep_for( long_wait );
  )
}

int main(int argc, char* argv[])
{
  TIME( "main",
    method1();
    method3();
  )

  return 0;
}

Here’s the output from the sample code:
Profiler

Leave a comment

Filed under C++, C++ Code, Programming

Idiot’s Guide to C++ Templates

This article on C++ templates is well written, if rather long!

The second part is here.

Leave a comment

Filed under C++, Programming

Online C++ Compilers

Today I took a look at some Online C++ compilers – this would have been very useful when checking portability of some code in my recent post on C++11 Concurrency.

First, here’s LiveWorkspace.org which makes g++ 4.72 and 4.8 available as well as clang 3.2:

MutableReferenceMemberClang

Second, Rise4Fun allows you to try Visual Studio:
MutableReferenceMemberVS

As anticipated, the Counter class (which contains a mutable member of type int&) compiles under Visual Studio, but doesn’t under clang 3.2 or gcc (which is correct according to the C++ standard). What’s great about LiveWorkspace is how quickly you can switch between compilers to see how they like the same code. For example, the amended code that treats int& as a template parameter compiles under g++ 4.7.2 and 4.8, but doesn’t under clang 3.2:

#include <iostream>

template<typename T>
class Counter
{
  mutable T m_t;
public:
  Counter(T t) : m_t(t){}
  void Increment() const { ++m_t; }
};

int main()
{
  int count = 0;
  Counter<int&> counter(count);
  counter.Increment();
  std::cout << count << "\n";
  return 0;
}

Leave a comment

Filed under C++

How to write managed C++ using templates

Occasionally, I write some managed C++ code as a glue-layer between C++ and F#. Today was such an occasion, and I found myself writing the same piece of code in several places with different types. Obviously, my reaction was to refactor to share the common code – given that the behaviour was common and independent of the underlying type, a template class seemed appropriate – but can you use templates with managed C++?

It turns out that you can – here’s the code, the aim of which was to take some COM object from native code and wrap it as an option of some expected strong type. If you haven’t seen managed C++ before, the syntax looks pretty ghastly – it may help to mentally substitute & for ^. If you aren’t familiar with F# option, it’s like boost::optional;.

template<typename T>
FSharpOption<T^>^ getOptional( IUnknown* raw )
{
  FSharpOption<T^>^ optionalValue = 
    optionalValue = FSharpOption<T^>::None

  if ( raw != nullptr )
  {
    T^ cooked = (T^)Marshal::GetObjectForIUnknown(IntPtr( raw ));
    if (cooked != nullptr)
      optionalValue = FSharpOption<T^>::Some(cooked);
  }
  return optionalValue;
}

Here’s how you would call the template function to get back the managed C++ equivalent of the F# type MyType option:

FSharpOption<MyType^>^ myValue = getOptional<MyType>( _rawValue );

Leave a comment

Filed under C++

Software code should read like well-written prose

I’ve long felt that well written software should not only be human-readable, it should be a good read. It’s a view held by Robert Martin who describes clean code as elegant, efficient, readable like well-written prose.

This came to mind when I finished reading a thriller, “The Lion” by Nelson DeMille. I’ve read several of his books, not least “The Charm School” which was brilliant. Now, The Lion didn’t have a great plot – in fact, having read a couple of his John Corey books already, I could pretty much predict the finale after the first couple of chapters. But it didn’t matter, because the story was so well told I enjoyed the journey. The next book I picked up is a science fiction tale set around the time of World War II but with a time-travelling twist – it has an exciting plot and I haven’t a clue what will happen. Yet it’s less enjoyable, the prose is stodgy and I’m struggling to get to know the characters.

Now, to me, the plot in a novel is analogous to software design, whereas the story telling is analogous to the implementation code. Whilst I’d prefer good story telling with a weak plot to a great plot with poor prose, the opposite is true of software – good design trumps good implementation code every time. That’s where software and fiction are so different – software is alive and will be maintained and extended throughout its life, whereas a novel is frozen in time the moment the author deems it finished. You can always re-implement badly implemented pieces of code – but it’s a much bigger task to re-work an entire design.

Leave a comment

Filed under Programming

Programming C++ on Linux Mint

First of all, let me say how easy it was to install CodeBlocks and CodeLite on Linux Mint using the Software Manager.  The user guide makes a terrific case for the Linux package management approach, but putting this UI on top (instead of using the command line as I was in Kubuntu) takes it to the next level.  For a start, it’s browsable and you can read reviews from other users.  I’ve installed CodeBlocks, CodeLite and g++.  I really like the way that the sections in Software Manager match the groupings from the start menu (so that my programming related applications are together and easily visible).

CodeBlocks
First impressions – not bad, it detected that I wanted to use gcc as my compiler.  I created a simple Hello World project and it compiled first time.  When I tried to add C++11 features, I got compile errors – but those were resolved by editing the Project build options and enabling “Have g++ follow the coming C++0x ISO C++ language standard”, i.e. -std=c++0x.  I rather like this dialog – it offers other useful compiler options too like “-Weffc++” to turn on Effective C++ warnings, with each compiler flag neatly explained.  As soon as I’d enabled -std=c++0x, the lambda and auto that I’d added compiled and ran just fine.

CodeLite

Oh dear.  Firstly, it offered to download a new version of CodeLite for me.  Given that I only just did it in Software Manager, that seemed a bit odd.  Next, I tried to create a new workspace – it crashed.  I tried to open the workspace it had created – crashed again.  This is probably because Software Manager needs to be updated to download the latest version, but I’ve uninstalled it for now.

Leave a comment

Filed under C++, Programming

Choosing a new brand of Linux

I started trying to upgrade my Kubuntu installation so that I could use gcc 4.7 and test the portability of some C++11 code I’ve been writing. So far, it’s not good news – despite having upgraded gcc and Eclipse, the standard library hasn’t upgraded (/usr/lib/c++ only contains 4.6) and trying to install libstdc++ results in obscure error messages.

So the time has come to try a new Linux distribution. Going back a year or so, installing Kubuntu was a chore – my Vaio laptop’s graphics driver wasn’t recognised and I recall making some low-level changes to get it working. Therefore, my main priority is to pick a Linux distribution with easy installation and that hints at good hardware support.

This article talks about Fedora, Mint and Bodhi. Funny that this could be a case of ‘better the devil you know’ – I know an Ubuntu derivative was a pain last time, but at least I did find examples of other people having the same issue as me! I’d never heard of Bodhi before, but it sounds ideal – very fast to install and is an Ubuntu derivative so should support my Vaio hardware.

As for an IDE, I was never very impressed with Eclipse, so I’ll try something else. Each of Codeblocks, NetBeans and CodeLite get good comments.

Leave a comment

Filed under Programming, Technology