This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[OT] exception performance cost


Hi all!

This is possibly OT, please tell me if discussion of
this is unwanted on this list.

I wanted some raw number telling me the performance difference
between a C++ code with exceptions and one with error return
code.

I wrote 3 very simple C++ programs to test it. I compiled it
with different flags and first checked the size differenz.
Using exceptions increase the file size with an amount of
6 - 10 %.

Then I checked execution time. I compiled all 3 programs with

g++-3.0 -Wall -Os -O2 -fomit-frame-pointer -march=i686 -o exception_performance3 exception_performance3.cpp

and stripped them afterwards.
I ran all 3 programs at least 5 consecutive times and calculated the average.
I used the "time" comand to measure the execution time.
The result was that using exceptions introduces less than 2 % performance loss.

Then I altered the 3 files and included 2 gettimeofday calls to measure the
ellapsed time. Surprisingly the situation flipped and now not using exceptions
costs ~3 % performance.

Has anyone an explanation for this?

I have attached the 3 source files.

matthias
#include <stdexcept>
#include <iostream>
#include <string>
#include <sstream>
#include <sys/time.h>

void
make_string( int i )
{
	std::string str;
	std::ostringstream stream;

	if ( i > 999999 )
	{
		throw( std::invalid_argument( std::string( "error " ) ) );
	}
	stream << "foo " << i;

	str = stream.str();
}

int
main( int argc,
      char * argv[] )
{
	struct timeval chrono1, chrono2;
	gettimeofday( &chrono1, 0 );
	
	for( int i = 1;
	     i < 1000001;
	     i++ )
	{
		try
		{
			make_string( i );
		}
		catch( std::invalid_argument & exc )
		{
			std::cout << "Bar: " << i << std::endl;
			break;
		}
	}
	
	gettimeofday( &chrono2, 0 );

	std::cout << "Elapsed :" << chrono2.tv_sec * 1000000 + chrono2.tv_usec - chrono1.tv_sec * 1000000 - chrono1.tv_usec << std::endl;
	
	return( 0 );
}

#include <stdexcept>
#include <iostream>
#include <string>
#include <sstream>
#include <sys/time.h>

void
make_string( int i )
{
	std::string str;
	std::ostringstream stream;

	if ( i > 999999 )
	{
		throw( std::invalid_argument( std::string( "error" ) ) );
	}
	stream << "foo " << i;

	str = stream.str();
}

int
main( int argc,
      char * argv[] )
{
	
	struct timeval chrono1, chrono2;
	int i = 1;

	gettimeofday( &chrono1, 0 );

	try
	{
		for( ;
	     	     i < 1000001;
	     	     i++ )
		{
			make_string( i );
		}
	}
	catch( std::invalid_argument & exc )
	{
		std::cout << "Bar: " << i << std::endl;
	}
	
	gettimeofday( &chrono2, 0 );
	std::cout << "Elapsed :" << chrono2.tv_sec * 1000000 + chrono2.tv_usec - chrono1.tv_sec * 1000000 - chrono1.tv_usec << std::endl;
	
	return( 0 );
}

#include <stdexcept>
#include <iostream>
#include <string>
#include <sstream>
#include <sys/time.h>

bool
make_string( int i )
{
	std::string str;
	std::ostringstream stream;

	if ( i > 999999 )
	{
		std::cerr << "Error: " << i << std::endl;
		return( true );
	}
	stream << "foo " << i;

	str = stream.str();

	return( false );
}

int
main( int argc,
      char * argv[] )
{
	
	struct timeval chrono1, chrono2;
	
	gettimeofday( &chrono1, 0 );

	for( int i = 1;
	     i < 1000001;
	     i++ )
	{
		if ( make_string( i ) )
		{
			break;
		}
	}

	gettimeofday( &chrono2, 0 );
	
	std::cout << "Elapsed :" << chrono2.tv_sec * 1000000 + chrono2.tv_usec - chrono1.tv_sec * 1000000 - chrono1.tv_usec << std::endl;

	return( 0 );
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]