lilburne wrote:
[color=blue][color=green]
>> PRECONCEIVED NOTION #3: The best implementations of exception
>> handling out there use the zero-runtime-cost model (I understand that
>> VC++ does
>> not, but gcc does). I understand that this model has no performance
>> penalties when not throwing because the stack is unwound using an
>> exteral table of ip-sorted elements rather than inserting code into
>> every function.[/color]
>
>
> Perhaps not. The last time I check GCC 3.3.2 was 5 times
> slower with exceptions than asserts:
>
http://groups.google.com/groups?&as_...c%241pao1h%241[/color]
40ID-203936.news.uni-berlin.de[color=blue]
>
> Performance seems to vary from compiler release to compiler
> release.[/color]
Either the gcc version for your platform has a bug related to exceptions
or your installation is somehow broken. Look at the following:
$ g++ --version
g++ (GCC) 3.3.2
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
$ cat point.C
#include <iostream>
#include <ctime>
#include <cmath>
#include <limits>
#include <cassert>
using namespace std;
class Point{
double m_x;
double m_y;
double m_z;
public:
Point() {
m_z = numeric_limits<double>::max();
}
Point(double x, double y, double z) {set(x,y,z);}
void set(double x, double y, double z) {m_x = x, m_y =
y; m_z = z;}
const double& x() const { return m_x;}
const double& y() const { return m_y;}
const double& z() const { return m_y;}
bool coincident(const Point& a, double eps) const;
bool is_valid() const {
return m_z != numeric_limits<double>::max();
}
};
#ifdef USE_EXCEPTIONS
bool Point::coincident(const Point& a, double eps) const
{
if (!is_valid()) {
throw int(1);
}
return abs(x()-a.x()) < eps &&
abs(y()-a.y()) < eps &&
abs(z()-a.z()) < eps;
}
void test(const Point& a, const Point& b)
{
try {
a.coincident(b, 1e-5);
}
catch (int) {
cout << "caught exception" << endl;
}
}
#else
bool Point::coincident(const Point& a, double eps) const
{
assert(is_valid());
return abs(x()-a.x()) < eps &&
abs(y()-a.y()) < eps &&
abs(z()-a.z()) < eps;
}
void test(const Point& a, const Point& b)
{
a.coincident(b, 1e-5);
}
#endif
int main()
{
time_t start = time(0);
Point a(10.0,10.0,20.0);
Point b(10.0,10.0,20.0);
for (int i = 0; i < 10000000; ++i) {
test(a,b);
}
cout << time(0) - start << endl;
return 0;
}
$ g++ point.C -O2 -DUSE_EXCEPTIONS
$ time ./a.out
0
real 0m0.371s
user 0m0.367s
sys 0m0.001s
$ g++ point.C -O2
$ time ./a.out
1
real 0m0.370s
user 0m0.366s
sys 0m0.002s
As you can see, your program isn't 5 times slower with exceptions than
without on my gcc 3.3.2 installation. The difference is actually
smaller than the measurement inaccuracy.