|
![](/i/fill.gif) |
clipka wrote:
> Severi Salminen <sev### [at] NOT THISsaunalahti fi invalid> wrote:
>> double x,y,z;
>>
>> do{
>> x = 2.0 * rNG.randomNumberClosed() - 1.0;
>> z = 2.0 * rNG.randomNumberClosed() - 1.0;
>> }
>> while(x*x + z*z > 1.0);
>>
>> y = sqrt(1.0 - (x*x + z*z));
>
> This can be optimized to:
>
> double x,y,z,rSquare;
>
> do{
> x = 2.0 * rNG.randomNumberClosed() - 1.0;
> z = 2.0 * rNG.randomNumberClosed() - 1.0;
> rSquare = x*x + z*z;
> }
> while(rSquare > 1.0);
>
> y = sqrt(1.0 - rSquare);
>
> Or maybe this is even a bit faster (and actually a tiny bit more defensively
> coded):
>
> double x,y,z,ySquare;
>
> do{
> x = 2.0 * rNG.randomNumberClosed() - 1.0;
> z = 2.0 * rNG.randomNumberClosed() - 1.0;
> ySquare = 1.0 - (x*x + z*z);
> }
> while(ySquare < 0.0);
>
> y = sqrt(ySquare);
>
No need to optimize the code, that's the job of the compiler. Any modern
compiler will notice the common subexpressions and optimize them. I just
did a small test with all three versions above compiled with "gcc -S
-O2" (gcc 4.1.2 on x86_64 linux). The produced assembly is virtually
identical for all three cases, the last one actually has the most
instructions generated (extra moves mostly).
The thing to learn from this, and it's my experience from work too, is
that you should write code that is easy to read and maintain rather than
try to be clever with optimizations.
When I'm on the subject of optimization I like to mention that profile
based optimizations can boost the performance of povray quite a bit.
Look at the -fprofile-generate and -fprofile-use options to gcc.
Sorry for going a bit off-topic. I'll go back to lurking now. I'm
following the development of the radiosity code with excitement. Keep up
the good work.
Oh, and happy new year!
--
Daniel Nilsson
Post a reply to this message
|
![](/i/fill.gif) |