I must have read the Foundation novels years ago and was intrigued to spot this sequel in the book shop. It’s a brilliant read, packed full of technology, philosophy, planetary adventures and even politics. Highly recommended.

Book Review: Foundation and Earth, Isaac Asimov
Filed under Book Review
Guru of the Week restarting from Item 1
Herb Sutter is reviewing his Guru of the Week series from item one, in the light of features in C++11 and C++14. This will form the backbone of a new issue of Exceptional C++.
Filed under C++, 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 < 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:

Filed under C++, C++ Code, Programming
How to stop Visual Studio 2010 showing the post-build Error List
One of my least favourite features of Visual Studio 2010 is that every time my build has an error, it pops up the Error List window. And every time, I switch to the Output window to read the full compiler error because the Error List doesn’t show enough information. In terms of interaction, that’s a major context switch – I’m focussed on the Output window looking at compile errors then BANG, the Error List interrupts my train of thought.
Now, I’ve tracked down this option to stop the Error List appearing:
The remaining question is: why put this option under “Projects and Solutions” instead of under “Build and Run”?
Filed under Programming
Algorithmic Interview questions (with solutions in F#)
InFSharpMajor.com is working through a book of algorithmic interview questions and solving them in F#. The questions come from this book which provides solutions in C++.
I think the problems and solution algorithms are interesting in their own right. I found the F# solutions a bit opaque (and I’ve been writing F# for several years), but my colleagues from a functional (*cough* Haskell) background tell me the idioms are well recognised in the community.
Filed under Programming, Soft skills
Idiot’s Guide to C++ Templates
This article on C++ templates is well written, if rather long!
The second part is here.
Filed under C++, Programming
C++14: Runtime-sized arrays
More details on the ISO C++ blog about runtime-sized arrays in C++14.
N3639 proposes to add local runtime-sized arrays with automatic storage duration to C++, for example:
void f(std::size_t n)
{
int a[n];
for (std::size_t i = 0; i < n; ++i)
{
a[i] = 2*i;
}
std::sort(a, a+n);
}
Traditionally, the array bound “n” had to be a constant expression.
Filed under C++, Programming
ISO C++ Spring 2013 Report
Herb Sutter has posted his trip report from the ISO C++ Spring 2013 meeting in Bristol.
The post includes details on features to be included in C++14, including:
std::make_unique– never use “new” again- Generic lambdas – allow auto for type name in lambdas
- Dynamic arrays – stack-based arrays can take size parameter at runtime
std::optional– for variables that are ‘not set’ (like F# option)- Concepts lite – constraints for templates
Filed under C++, Programming
Google task monitoring plans
The Register reports on Google’s approach to active task monitoring and management:
“Our solution, CPI2, uses cycles-per-instruction (CPI) data obtained by hardware performance counters to identify problems, select the likely perpetrators, and then optionally throttle them so that the victims can return to their expected behavior. It automatically learns normal and anomalous behaviors by aggregating data from multiple tasks in the same job.”
The article talks about the benefits in terms of low-latency guarantees, because hungry batch jobs can be killed. The data might be useful for tracking down performance bottlenecks too – imagine never having to attach a profiler to a running process because you can already attribute the time spent on a job to its individual tasklets.
Filed under Technology
The Mythical Man-Month
The Guardian cites The Mythical Man-Month as mandatory reading for anyone planning a big software project. I’d agree, as well as recommending Peopleware (Tom DeMarco & Tim Lister) and Slack (Tom DeMarco)
Filed under Programming
