Davidsen's device
William Davidsen
davidsen at sungod.crd.ge.com
Wed Aug 2 04:07:50 AEST 1989
I recently discovered the following technique. While I can't swear
that it's new and unique to me, I can claim independent discovery. I
make no claims that it is suited to any problem you have, will have, or
would admit to having.
I was calculating a function and had two algorithms, one of which was
13 times faster than the other but unreliable for input values less than
1.0, the other very accurate but slow. I had to evaluate the function
6E8 times on a slow machine.
Here's the code:
double hacs1(), hacs2();
double asect;
(*(asect < 1.0 ? hacs2 : hacs1)(asect);
Since the name of a function is typed "pointer to function
returning..." this was treated as "(*ptr)()" and generated the desired
code. It was slightly faster than the obvious:
if (asect < 1.0) hasc2(asect);
else hasc1(asect);
bill davidsen (davidsen at crdos1.crd.GE.COM)
{uunet | philabs}!crdgw1!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me
More information about the Comp.lang.c
mailing list