Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

division... slow?

Name: Anonymous 2006-09-25 16:25

In a lot of videogame code, whenever a lot of numbers are to be divided by one number, I usually see this:

inv = 1.0 / divisor;
...and then all the numbers * by inv

rather than all the numbers being / by divisor.

I'm guessing, at the CPU level, division is slightly slower and less efficient than multiplication?  I can't really think of any other reason why someone would do this.

Name: Anonymous 2006-09-26 17:12

>>1-7
$ cat multtest.c;echo --;cat divtest.c
int main() {
        float inv;
        float i;
        inv=1.0/42;
        for(i=0;i<1048576;i++)
                printf("%f\n",i*inv);
        return 0;
}
--
int main() {
        float i;
        for(i=0;i<1048576;i++)
                printf("%f\n",i/42);
        return 0;
}
$ gcc -O3 -msse3 -ffast-math multtest.c -o multtest
$ gcc -O3 -msse3 -ffast-math divtest.c -o divtest
$ ./multtest|md5
f17a45565039beca9f5ed6ff27b17afd
$ ./divtest|md5
f17a45565039beca9f5ed6ff27b17afd
$ time -h ./multtest>/dev/null
        4.07s real              2.97s user              0.01s sys
$ time -h ./divtest>/dev/null
        4.07s real              2.97s user              0.01s sys

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List