Cleaning out “using namespace std;” declarations from header files.

I recently had to work with a very large codebase, in which each and every file included a header file with the statement “using namespace
std;” in it.
This led to the situation that hundreds of header files, using std strings, pairs, etc, were using those items without any std:: dereferencing.

Cleaning this situation up by hand would have taken weeks and been error prone, so I wrote a little script to do it for me, and called it standardize.pl

The variable possible_offenders is a list of std c++ names which are frequently in place in the code in question.

The script recursively searches a directory for for .h and .cpp files. For cpp files, it checks if any of the possible_offenders occur in the file.  If so, it adds a “using namespace  standard” directive if none exists.  Thus cpp files are changed minimally.

For header files, all occurences of “using namespace std” are removed, and all occurences of possible_offenders are prefaced by an explicit std:: namespace specification.  Care is taken not to change occurences in comments or in quotations.

If you are faced with a similar situation, you can find the script on github: https://github.com/spacemoose/standardize

cout, buffering, and premature pessimizations.

The other day I was in a discussion with some c++ developers, where one of them stated, most definitavely, that “cout is not buffered”.   Now I have to admit that I was flabbergasted by how wrong this assertion was, and my first instict was to question the capabilities of the developer in question.  As I look back on the vast majority of the code that I have worked with however, it’s pretty clear that most c++ developers are either unaware that cout is indeed buffered, or they are unaware of the side effect of std::endl, or they just don’t think about the impact it causes.  Consider the following two lines of code:

  std::cout << "some text" << std::endl;
  std::cout << "some text\n";

Now, neither of these lines of code is more readable than the other.  Neither is more maintainable than the other.   The endl variant can however take significantly more time to execute than the \n variant. Why? Because std::endl has two effects:

  1.  It inserts a ‘\n’.
  2. It iinserts a std::flush into the stream, flushing the buffer.

If you are using a std::endl where a ‘\n’ will do (i.e. you do not need to explicitly flush the buffer), you are creating what Sutter and Alexandrescu call a “premature pessimization” in their excellent book “C++ coding standards”.   Despite this, the endl variant is much more common.  Whenever I ask anyone why they are using endl’s all over the place instead of \n,, the typical answer is “well, it’s more the C++ way to do things”.  That’s just not true — it’s not the C++ way to do meaningless resource comsumption.

Rule 1 is “don’t optmize prematurely.”  This means you should not make your code less readable, more complex, or less maintainable for the sake of dubious performance benefits.  A correllary to this rule however is “don’t pessimize prematurely”.  If two variants are equally readable, equally clean, and equally maintainable, prefer the more efficient variant.  This is just a question of correcting ignorance and forming good habits.

So you might be curious if this performance difference is measurable, and the answer is of course it is.  You can test it yourself with the following benchmark:

#include <iostream>
#include <chrono>

namespace chrono = std::chrono;

int main ()
{

	constexpr unsigned int numLines = 100000;
	auto start = chrono::high_resolution_clock::now();
	for (unsigned int i =0; i< numLines; ++i)
	{
		std::cout << "This is a prematurely pessimized line" << std::endl;
	}
	auto pess = chrono::high_resolution_clock::now();
	for (unsigned int i=0; i<numLines; ++i)
	{
		std::cout << "This is not a prematurely pessimized line\n";
	}
	std::cout << std::endl;   // flush the buffer so the comparison is
							  // only biased in favor of pessimized
							  // code.
	auto np = chrono::high_resolution_clock::now();
	double durp = chrono::duration_cast<chrono::milliseconds> (pess-start).count();
	double durnp = chrono::duration_cast<chrono::milliseconds> (np - pess).count();
	/// Use cerr for benchmark results, so we can redirect the noise.
	std::cerr << "\n==============================\n"
			  << "pessimized code took: " << durp << "ms.\n"
			  << "unpessimized  took  : " << durnp << "ms.\n"
			  << "Buffering saved: " << durp-durnp << "ms., or " << 100* (durp-durnp)/durp
			  << "% speedup." << std::endl;

}

Compiling on gcc with -O2, I get the following results:

Output to termial:

==============================
pessimized code took: 5370ms.
unpessimized  took  : 4806ms.
Buffering saved: 564ms., or 10.5028% speedup.
~/Personal/Miscelaneous[master]$ ./a.out > /dev/null

Output to /dev/null
==============================
pessimized code took: 45ms.
unpessimized  took  : 6ms.
Buffering saved: 39ms., or 86.6667% speedup.
~/Personal/Miscelaneous[master]$ ./a.out > tmp

Output to file:

==============================
pessimized code took: 365ms.
unpessimized  took  : 79ms.
Buffering saved: 286ms., or 78.3562% speedup.

If you are writing code which uses output streams a lot, like logger functionality, or file output, this can make a huge difference to your resource consumption, and you’ll never see the needless waste in a profiler.  So form good habits.  Unless you need to flush the buffer for some reason (which in fact is a rare need unless you’re dealing with concurrency issues), prefer the \n construct.

 

Swiss nonconfrontationalism.

I have been living in Switzerland since September of 1996. At time of writing I have been living in Switzerland for 17 years. I would estimate I have lived in the United States for 14 years, and Canada for 12 years. So alltogether I have lived in the Canmerican culture for 26 years, some 9 years more than I have lived in Switzerland. This gives me an unusual perspective on the three cultures.

I have frequenlty heard the complaint that Swiss people are averse to confrontation, and as a result never say anything directly. This can manifest itself in wierd ways, like leaving notes in the laundry room saying “So bitte nicht!” (not like this!). I hear this criticism from Swiss people as much as I have heard it from fellow foreigners. People often make a conceptual connection to Switzerland’s political neutrality, which I never came to myself. I’m a big fan of Switzerland’s political neutrality. That national characteristic is a large part of why I feel at home here. I actually get a bit resentful towards foreigners who live here for the money, but despise Switzerland’s neutrality.

Until recently though I have still been critical of Switzerland’s aversion to confrontation at a personal level. For an American it makes navigating relationships with a Swiss partner fraughtful. Sometimes it seems the characteristic is taken to some cartoonish extreme — a case where this characteristic is possessed to such a cartoonish extreme as be unbelievale (at least to someone from without the culture). This can lead to some pretty bizarre relationship stories. As a result of collecting some negative experiences I could quite relate to people expressing resentment of this trait.

Now I find myself in a wonderful, happy relationships with a Swiss woman. While she is an impressive woman in her own right as an individual, some of the things that I love most about are quite “typically Swiss”. In fact she takes a real pride in being Swiss, and cherishes Swiss culture in a deep and meaningful way. She is wise in her love of her culture. She doesn’t love her country and culture indescriminately. She confronts many of the very real problems that Swiss culture does possess in a very head on and responsible way. Besides making her a remarkable human being, this characteristic makes her a remarkable teacher. The integration of Switzerland’s overwhelmingly large immigrant population is a real and pressing issue, and her ability to love and critisize her country simultaneously and intelligently, is a real gift to her students.

So my love and respect for my wife makes me look at certain aspects of Swiss culture in a more loving and respectful way. While Bettina’s tendency to avoid confrontation is afrequent topic of discussion in our family, my tendency to dive directly into conftration is of equal concern. I believe though that I have more to learn (and have learned more) from the Swiss characteristic. By now I have worked with a number of Swiss managers, and I believe that the most effective of these also pursued this path aversion-to-conflict.

The problem is that saying “aversion to conflict” isn’t really precise enough. At a cultural level the desire is avoid emotions flaring up durign a conflict, which would disturb the problem solving process. As I write that I find that I can’t help but think of Swiss political neutrality. So now that I am able to love them both, I see these traits as beeing two aspects of a single trait. When I resented one and loved the other I was unable to see the connection. I find that fascinating.