Konstantin Olchanski wrote:
> On Thu, Oct 08, 2009 at 07:59:38PM -0700, Suvayu Ali wrote:
>> My code looked something like this,
>>> double pi = acos(-1);
>>> float delta_phi2 = fabs(2*TMath::Pi - delta_phi);
>
>
> Traditionally, (early C, Fortran, etc), one is supposed to write
> acos(-1.0) and
> fabs(2.0*TMath::Pi)
>
> because early compilers did not know to automatically convert
> into to double (they did not know fabs() and acos() take
> an argument of type double).
>
> Also instead of TMath::Pi you can say
>
> #include <math.h>
> fabs(2.0*M_PI) <--- this becomes a compile-time constant
>
>
> You also may have 2 performance bugs, which may or may not be important
> depending on the use of the code:
>
>> double pi = acos(-1);
>
> Invokes the acos() function, which tends to be pretty expensive,
> even on CPUs with hardware acos().
>
>> fabs(2*TMath::Pi())
>
> Invokes the Pi() function every time. If you say 2.0*M_PI, it becomes
> a compile-time constant (I am not sure if GCC can fold fabs(constant)
> into a constant, though).
>
> Both problems, if they are problems, should show up on in
> the profiler (gprof & co) as excessive number of calls to acos(), fabs() and Pi().
>
Thanks Konstantin, Rene and Philippe for your replies. The note on
performance issues was very enlightening. I will try to be mindful
henceforth.
--
Suvayu
Open source is the future. It sets us free.
Received on Fri Oct 09 2009 - 20:37:38 CEST