why is long double so slow?

Jan 22, 2011 at 10:48pm
The following code is at least a factor for 10 slower with F typedefed to long double as opposed to double. Simpler loops only seem to take a 30% hit. Anyone have any clues why that might be?

I thought long double was just extended precision and one should take a memory hit but not so much an execution time hit.

include <math.h>
typedef FTYPE F;
namespace audelsi
{
void cholesky_ut(F *A, int n)
{
int i, j, m, nj;
F *jm, *ij, *im, *jj, d;
for (j = 0, nj = n, jj = A; j < n; j++, nj--) {
for (m = j, jm = jj; m < n; m++) {
for (i = 0, ij = jj, im = jm, d = *jm; i < j; i++) d -= *(ij -= nj) * *(im -= nj);
*(jm++) = d;}
for (m = j + 1, d = *(jj) = sqrt(*jj), jj++; m < n; m++) *(jj++) /= d;}}}

g++ (GCC) 4.4.4 20100630 (Red Hat 4.4.4-10)
core 2 quad Q9400 @ 2.66GHz

Jan 22, 2011 at 11:52pm
In order to calculate with higher precision more digits are used, and more steps must be taken. Consider these two statements:
4.39*5.29 = 23.22
4.4*5.3 = 23.3
Which one would take more steps by hand? The same goes for a computer.
Jan 23, 2011 at 12:24am
Sure its longer but a factor of 10 for the extra two or three decimal digits?

Supposedly you almost get the few extra digits for extended precision for free because the
chip does that anyway. Then it rounds to get to double so it kind of does the computing
at long double and then throws the extra precision.

I think there must be something else. I wondered whether the extra digits might be
causing the whole computation to go out of cache but reduced the dimensions and so that should not be a problem. Then
I wondered if maybe my way at going at it is just obviously braindead or possibly there
are g++ switches badly set ...

Anyway the computation actually works so there is not likely a pointer out of control
or anything like that.
Jan 23, 2011 at 2:57pm
What is sizeof( long double ) on your system?
Jan 24, 2011 at 7:34am
sizeof( long double )=16
sizeof( double )=8

That's not what I expected but its a 64 bit system and I think I might have
read that's the way it goes.

Anyway, I apologize. I was wrong about the thing working. It was not. I was sure
but the little part that computed the error was really wrong and it told me all was
well but it was awfully wrong. But now it is working, and
long double goes 170% the time of double, which is much more what I expected. Problem solved.

Thanks.
Topic archived. No new replies allowed.