I have inherited some plain old C code written back in '94 that uses
only floats. I did a little empirical testing on my Mac (2 x 2.8 GHz
Quad-Core Intel Xeon) to see whether it would be worthwhile to change
everything to double. I would like the greater accuracy, but don't
want any kind of performance hit.
I'm using Xcode 3.0 with gcc 4.0.
My first experiment was to take a look at the number of information-
bearing digits with float vs. double:
printf("float: %.30f\n",(float)1/9);
printf("double: %.58lf\n",(double)1/9);
Result:
float: 0.111111111938953399658203125000
double: 0.1111111111111111049432054187491303309798240661621093750000
>> Question: I was a bit surprised to see a substantial amount of (what
looks like) garbage in the least significant digits of these values -
should I be?
>> My next experiment was to measure latency. Here's the code:
void ASG_fd_timing_test(void) {
ASG_TIMER timer;
long num_iter;
float a_f,b_f,c_f,d_f,e_f,f_f;
double a_d,b_d,c_d,d_d,e_d,f_d;
int duration_float;
int duration_dble;
int n;
printf("Number of iterations? ");
scanf("%ld",&num_iter);
// --- start timer ---
ASG_timing_init(
&timer);
ASG_timing_stamp_advance(
&timer);
// --- do float testing ---
a_f = 1.6;
b_f = -0.33;
for (n=0; n<num_iter; n++) {
c_f = a_f + b_f;
d_f = a_f - b_f;
e_f = a_f * b_f;
f_f = a_f / b_f;
a_f += 3.2;
b_f -= 1.05;
// -- record stop time --
ASG_timing_stamp_advance(
&timer);
// --- do double testing ---
a_d = 1.6;
b_d = -0.33;
for (n=0; n<num_iter; n++) {
c_d = a_d + b_d;
d_d = a_d - b_d;
e_d = a_d * b_d;
f_d = a_d / b_d;
a_d += 3.2;
b_d -= 1.05; }
// -- record stop time --
ASG_timing_stamp_advance(
&timer);
// -- find float duration --
ASG_timing_find_delta(
&timer.timestamp[0],
&timer.timestamp[1],
&duration_float);
// -- find double duration --
ASG_timing_find_delta(
&timer.timestamp[1],
&timer.timestamp[2],
&duration_dble);
printf("\nDuration float, double = %d %d ms
\n",duration_float,duration_dble);
}
The results:
Number of iterations? 1000000000
Duration float, double = 359 358 ms
>> It appears that using doubles has no performance hit. If I took a look
at the ASM code and knew more about Intel processors, perhaps I would know
why. My tentative conclusion is that I should go ahead and upgrade. Any
comments?
TIA,
Steve


|