RE: [Algorithms] C style math libraries versus "proper" C++ maths

"Stephane Etienne" <[email protected]> Sat, 25 Mar 2006 14:00:55 -0800
Newsgroups gmane.games.devel.general
Message-ID <[email protected]>
To do a fair comparison, we would need to see your C implementation. You
implemented operator+, operator*, etc but these return a temporary which
may or may not be optimized out. The best way to find out is to look at
the disassembly. Also, different compiler may do different things. If
your C version usage does not need any temporaries, then it will most
likely be faster indeed. However if u use operator+= or operator *=, you
should definitely get similar performances. Also never profile debug
builds. Even functions marked inline will not be inlined in debug builds
for example.

 

Since C99 support the inline keyword, you should not see any difference
between a C and C++ implementation. As Jarkko said, beware of
exceptions. They can easily make your code a lot slower.

 

I have a few suggestions regarding your code. They will not improve
execution speed but you wrote several things that are unnecessary:

 

-          the inline keyword is not necessary for inlined method inside
the class body (less to read means it is also easier to read)

-          Initializer list are only useful when the default constructor
of constructed objects actually does something. Your use of initializer
list here does not buy you anything.

-          Prefer C style functions over C++ member methods. See Scott
Meyer, "How non member functions improve encapsulation" for more info.

-          No need to have a default destructor that does not do
anything for non virtual classes.

-          No need to implement the default copy operator unless you
need to do something special. In your case, the default copy
constructors and operators provided by the compiler will do just fine.

-          Your use of statics to implement zero vector, etc could mess
with the cache on platforms such the Xbox 360 and the PS3.

 

Understand your code. Look at the disassembly and profile over an over
again to understand why something is slow.

 

Stephan.

 

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of
Richard Fabian
Sent: Saturday, March 25, 2006 5:40 AM
To: [email protected]
Subject: [Algorithms] C style math libraries versus "proper" C++ maths

 

We've always used a math library that has a function per operation you
want to apply to each math construct. We do this because "back in the
day" we found that C++ style math libraries lead to lots of unecessary
destructors being made... at least in debug, but we suspected in release
also. 

its been a few years, two major microsoft compilers later, a new SN
compiler for PS2, and GCC still getting better and better...

so, after hearing a workmate claim that he was using a C++ math library
"because it was faster" i decided to update my old C++ math library and
make myself a profiling testbed. 
The sad news is: my old and horrible to read and work with function
based math library still outstrips the C++ math library by about a 5%
speed margin, even on trivial stuff.

I'm not a C++ newb, i do know about constructor return optimisation,
operator overloads copy constructors and the like, but as i tend to
write code that is more architectural than the low level stuff like math
libraries, i wondered what kind of performance difference (and which
way) you were seeing. 

neither math library uses any platform specific optimisations, such as
SSE, SIMD, VU. This is pure C++ only.

I have pasted my C++ math "header" that i was using for profiling if you
want to take a look at my code and maybe point me in a direction of
betterly optimised code..

http://rafb.net/paste/results/P2kY2O59.html