I just put everything together in one file. And as the benchmark
is called with a function-pointer twice inteprocedural optimiza-
tions aren't really needed anymore to prevent the compiler from
optimizing away the throw.
I compiled this with an older gcc and found, that gcc is about
42% faster when trowing an exception; and theres a big differnece
to VC++ because it takes almost the same time cathing a derived
exception than catching the base-exception.
#include <iostream>
#include <chrono>
#include <exception>
using namespace std;
using namespace std::chrono;
struct ExcBase : public exception
{
};
struct ExcDerivedA : public ExcBase
{
};
struct ExcDerivedB : public ExcDerivedA
{
};
typedef long long LL;
void BaseThrower();
void DerivedBThrower();
LL Benchmark( void (*thrower)(), unsigned const rep );
int main()
{
unsigned const REP = 1'000'000;
LL ns;
ns = Benchmark( BaseThrower, REP );
cout << "BaseThrower: " << (double)ns / REP << "ns" << endl;
ns = Benchmark( DerivedBThrower, REP );
cout << "DerivedBThrower: " << (double)ns / REP << "ns" << endl;
}
LL Benchmark( void (*thrower)(), unsigned const rep )
{
time_point<high_resolution_clock> start = high_resolution_clock::now();
for( unsigned i = rep; i; --i )
try
{
thrower();
}
catch( ExcBase & )
{
}
return (LL)duration_cast<nanoseconds>( high_resolution_clock::now()
- start ).count();
}