Related articles |
---|
[11 earlier articles] |
Re: Why is using single-precision slower than using double-precision kenneta@hubcap.clemson.edu (1994-11-23) |
Re: Why is using single-precision slower than using double-precision dik@cwi.nl (1994-11-24) |
Re: Why is using single-precision slower than using double-precision davidc@panix.com (David B. Chorlian) (1994-11-24) |
Re: Why is using single-precision slower than using double-precision roedy@BIX.com (1994-11-30) |
Re: Why is using single-precision slower than using double-precision tgl@netcom.com (1994-11-30) |
Re: Why is using single-precision slower than using double-precision hebert@prism.uvsq.fr (1994-11-24) |
Re: Why is using single-precision slower than using double-precision dekker@dutiag.twi.tudelft.nl (Rene Dekker) (1994-11-30) |
Re: Why is using single-precision slower than using double-precision meissner@osf.org (1994-11-24) |
Newsgroups: | comp.parallel,comp.arch,comp.compilers |
From: | Rene Dekker <dekker@dutiag.twi.tudelft.nl> |
Keywords: | C, arithmetic |
Organization: | Delft University of Technology |
References: | <3aqv5k$e27@monalisa.usc.edu> |
Date: | Wed, 30 Nov 1994 06:26:21 GMT |
Zhiwei Xu <zxu@monalisa.usc.edu> wrote:
> Can any one explain why a C program using single precision (float) is slower
> that the same code using double precision (double)? Please try the following
> code for computing pi.
Double is specified to be the 'most natural floating point size' in C
and so it is not suprising that computations in double are faster than
computations in float. Float is primarily ment to save memory space,
not to gain speed.
Apart from that, constants in C are double by default. Therefore, given
the main loop in your program:
> /* #define double float */
> for(i=1;i<=N;i=i+1) {
> local = ( ((double) i) - 0.5 ) * w ;
> pi = pi + 4.0 / ( 1.0 + local * local ) ;
> }
The computation are done in double precision anyway, whatever you
define 'double' to be, because all the constants are doubles. If you
define 'double' to be 'float', then all the variables have to be cast
to doubles before computation, and that's what takes the extra time.
I changed your program to the following, and got better speeds for
floats, both on Sun4 and on Linux. Be sure to use an ANSI
compliant compiler, because otherwise you are not guaranteed the
computations are done in float precision, even if all the players are
float.
-------------8<-------------snip-snap-snip------------->8-------------
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/times.h>
#include <sys/time.h>
/* #define double float */
int N = 2000000;
int main( void )
{
struct tms begin_time, end_time;
struct timeval tv1, tv2;
double kern_time, user_time, local, pi=0.0, w ;
long i, j, t;
times( &begin_time );
gettimeofday( &tv1, NULL ); /* before time */
w = 1.0 / N ;
for( i = 1; i <= N; i = i + 1 ) {
local = ( i - (double) 0.5 ) * w ;
pi = pi + (double) 4.0 / ( (double) 1.0 + local * local ) ;
}
gettimeofday( &tv2, NULL ); /* after time */
times( &end_time );
t = (tv2.tv_sec - tv1.tv_sec) * 1000000 + tv2.tv_usec - tv1.tv_usec;
kern_time = (end_time.tms_stime - begin_time.tms_stime) / (double) CLK_TCK ;
user_time = (end_time.tms_utime - begin_time.tms_utime) / (double) CLK_TCK ;
printf( "pi is %f \n", pi*w );
printf( "the kernel time is %f seconds\n", kern_time );
printf( "the user time is %f seconds\n", user_time );
printf( "the user MFLOPS is %f \n", 6 * (N/1000000.0) / user_time );
printf( "the wall clock time is %d uSecs\n", t );
printf( "the wall MFLOPS is %f \n", 6 * N / (double) t );
return 0;
} /* main() */
-------------8<-------------snip-snap-snip------------->8-------------
Ciao,
Rene'
--
Rene Dekker Delft University of Technology
R.Dekker@twi.tudelft.nl Department of Technical Mathematics and Informatics
Tel: +3115 783850 Julianalaan 132
Fax: +3115 787141 2628 BL Delft, The Netherlands
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.