[erlang-questions] Erlang arithmetics

Richard O'Keefe ok@REDACTED
Mon Nov 1 04:16:21 CET 2010


On 30/10/2010, at 10:55 PM, Morten Krogh wrote:

> Sorry, I just realized that you are only timing the inner loop, not the random number generation.
> 
> But why bother with the random numbers then? Shouldn't multiplication and addition run with the same speed for all values of numbers of a given type?

No.

For example, according to
http://pdos.csail.mit.edu/6.828/2007/readings/i386/IMUL.htm
"The 80386 uses an early-out multiply algorithm.
 The actual number of clocks depends on the position of the most
 significant bit in the ... multiplier."

Look for "early-out multiply".

Since these numbers were the results of random:uniform(1000)
they should be particularly nice cases for an early-out multiplier.
Had they been random:uniform(1000000) the products would have
been bignums on a 32-bit machine and thus *much* slower;
bignum operations *definitely* depend on the size of the number.

Nor can you expect the cost of + and * to be uniform for
floats.  Operations that overflow to +/-infinity may well be
*very* expensive on some machines.  One popular implementation
strategy for IEEE arithmetic was to do the easy cases in hardware
and let the operating system finish the tough jobs.



More information about the erlang-questions mailing list