comparative efficiency of floats / doubles

Started by
3 comments, last by glJunkie 22 years, 2 months ago
Hi everyone. In C++, which are faster: calculations using floats or calculations using doubles? Is it true that calculations using floats are actually done at double precision and then truncated - effectively meaning you might as well use doubles for calculations? I hope someone can help with this - Ive been confused about it for a while... Thanks! Tim.
Advertisement
Yes, internally the fpu of x86 class chips does floating point math at 80 bits (long double). Of course you can change the fpu default setting to work at 32 bits (float) and you gain some speed. But mainly the advantages of using floats over doubles is that you save space, 32 bits for a float vs. 64 bits for a double. And in memory limited architectures like our good ol'' pc, we need all the memory bandwidth we can get.
If you stick with floats, you may be able to use SSE; I think doubles need SSE2 which hasn''t got anything like the same market penetration.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
Thanks for the replies,

Premandrake,
By "change the fpu default setting to work at 32 bits (float)" do you mean simply decalring variables as float? And making sure to declare numbers with an "f", eg:

float i = x + 0.2f;

Also, what is SSE?

Well... he doesn''t mean to explicitly FLOAT a value and suffixing and ''f''... he means setting the internal precision of the FPU by using the _control87 or _controlfp function. These functions will change the internal behaviour of the FPU unit in your processor.

Then... SSE is an instruction set for Intel Processors (I believe from Pentium III and upward, but I am not 100% sure) that gives some extra multimedia extensions, under which there are some fast floating point ops (but on this subject I am not 100% sure also... just look it up at www.intel.com)

Finally... to answer your question. I once had the same question... so I built a little benchmark. The results aren''t that surprising...

1) In software emulation of FP, 32 bits (ie floats) are much faster than 64 bits (ie double).

2) In the hardware FPU, 32 bits (ie floats) are a little bit faster than doubles... but not very significantly (about 7% or so)... this is because the FPU always has to copy the value from its internal register to some memory you own... and copying 4 bytes seems faster than 8 (which is logical). The fact that internal truncation occurs doesn''t seem to matter that much at all.

Conslusion... Note that everything I stated here MIGHT NOT WORK FOR YOU!! Ie I tested it all... but my tests could be wrong or the results could be processor specific (I used an AMD Athlon 900). But at least this might have answered your question a bit.

ICQ: 130925152
Email: e.j.folkertsma@student.utwente.nl
ICQ: 130925152Email: e.j.folkertsma@student.utwente.nl

This topic is closed to new replies.

Advertisement