Re: C style math libraries versus "proper" C++ maths

Andras Balogh <[email protected]> Sat, 25 Mar 2006 15:26:10 -0700
Newsgroups gmane.games.devel.general
Message-ID <[email protected]>
With regards to inlining, I believe that the inline keyword is merely
a suggestion to the compiler. I've found that some compilers might not
inline your code even in optimized release builds, when it's
explicitly specified as inline. If you really know that a function
should be inlined, you might want to use _forceinline or something
similar.

One good example is when your function is complicated, but given a
constant input, it evaluates to a constant result, with no
side-effects (in my case this was a function computing perlin noise).
Using the inline keyword, the compiler made the function call, running
an expensive function. With __forceinline, it could actually compute
the result at compile time and get rid of the function call
altogether!

That said, be very careful, when using __forceinline!




Andras


Saturday, March 25, 2006, 3:00:55 PM, you wrote:

> To do a fair comparison, we would need tosee your C
> implementation. You implemented operator+, operator*, etc but
> thesereturn a temporary which may or may not be optimized out. The
> best way to findout is to look at the disassembly. Also, different
> compiler may do differentthings. If your C version usage does not
> need any temporaries, then it will most likely befaster indeed.
> However if u use operator+= or operator *=, you should definitely
> get similarperformances. Also never profile debug builds. Even
> functions marked inlinewill not be inlined in debug builds for
> example.

>  

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

>  

> I have a few suggestions regarding yourcode. They will not
> improve execution speed but you wrote several things thatare
> unnecessary:

>  

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

> -          Initializer list are only useful when the default
> constructor of constructedobjects actually does something. Your use
> of initializer list here doesnot 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 fornon virtual classes.

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

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

>  

> Understand your code. Look at thedisassembly 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, 20065:40 AM
> To:[email protected]
> Subject: [Algorithms] Cstyle math libraries versus "proper" C++ maths

>  

> We've always used a math library that has a functionper 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 lotsof
> unecessary destructors being made... at least in debug, but we
> suspected inrelease also. 

> its been a few years, two major microsoft compilers later, a
> newSN 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
> libraryand make myself a profiling testbed. 
> The sad news is: my old and horrible to read and work with
> function based mathlibrary still outstrips the C++ math library by
> about a 5% speed margin, evenon trivial stuff.

> I'm not a C++ newb, i do know about constructor return
> optimisation, operatoroverloads copy constructors and the like, but
> as i tend to write code that ismore architectural than the low level
> stuff like math libraries, i wonderedwhat 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
> ifyou want to take a look at my code and maybe point me in a
> direction of betterlyoptimised code..

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

>  









-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Gamedevlists-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gamedevlists-general
Archives:
http://sourceforge.net/mailarchive/forum.php?forum_id=557