Which is faster? ulong or double

Matti Niemenmaa see_signature at for.real.address
Thu Sep 27 12:35:15 PDT 2007


Janice Caron wrote:
> I have this app I've written, and it needs to keep track of an integer
> quantity (time in microseconds, as it happens, but that's an
> unimportant detail). The point is, there are circumstances where the
> numbers involved get bigger than uint.max.
> 
> So the question is, given that I'm using a 32-bit platform, should I
> switch to ulong, or to double?
> 
> ulong sounds the most logical, since the quantity will always be an
> integer, but (correct me if I'm wrong) ulongs are emulated in
> software, which is fine for add and subtract, but not so fine for
> divide; whereas doubles have direct hardware support, and so might
> actually end up being faster if there are lots of divides.
> 
> Am I talking nonsense? Is there a recommendation?

One thing to consider is floating point error, which you won't get with ulongs.
You'll have to manually floor the values if you need integer division behaviour.
(And even then, things might get odd. I'm not an expert on floating point.)

Also, the x86 (I)MUL instructions result in 64-bit values, so at least
multiplication is provided by the hardware.

And AFAIK GDC can generate 64-bit code, so if you really need speed you can try
that.

I'd just use ulong and if you find it too slow, benchmark it versus doubles. And
reals, too.

-- 
E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi



More information about the Digitalmars-d mailing list