Just out of couriosity i made a benchmark test between C and C++ witherm, sorry, but why not use pointers?
gcc3. I dont have a clue abour x86 assembler so i made a measurement.
Here is the C code (not realy useful as real code would have a need for a
struct and a pointer operation to call the filter() function) and the
C++ code.
Both "simulate" a low pass filter and are compiled with:
gcc -O2 -march=pentium -o filter filter.xx
-O3 with C is broken, i got an endless loop!
---------
double x1,y1,a,b;
const double filter(const double x)
{
register double y;
y = a*(x + x1) - b*y1;
x1 = x;
y1 = y;
return y;
}
int main()
{
double x=1;
int i;
x1 = y1 = 0;
a = b = 0.5;
for (i=0; i<1000000000; ++i) {
x = filter(x);
}
}
---------
class LowPass {
public:
LowPass() { x1 = y1 = 0; a = b = 0.5; };
~LowPass() {};
const double filter(const double x);
private:
double x1,y1,a,b;
};
inline const double LowPass::filter(const double x)
{
register double y;
y = a*(x + x1) - b*y1;
x1 = x;
y1 = y;
return y;
}
int main()
{
//LowPass* LP = new LowPass();
LowPass LP;
double x=1;
for (int i=0; i<1000000000; ++i) {
//x = LP->filter(x);
x = LP.filter(x);
}
}
---------
The results on my AthlonXP machine are:
C++ with member:
real 0m11.847s
user 0m11.850s
sys 0m0.000s
C++ with new() and pointer:
real 0m12.337s
user 0m12.330s
sys 0m0.000s
C:
real 0m16.673s
user 0m16.670s
sys 0m0.000s
Well, i will stay with pointer less C++ :-)
- Stefan
_________________________________________________________________
Surf the Web without missing calls! Get MSN Broadband. http://resourcecenter.msn.com/access/plans/freeactivation.asp