|
|
|
|
|
by bjourne
3643 days ago
|
|
Everything is relative. :) Compared to the add instruction itself, the overflow check is very expensive. But the big problem is that your types widen. E.g: // x and y are hardware integers >= 0
var z = x + y
for (var i = 0; i < z; i++) {
...
}
If your language used modular arithmetic, 'z' would also be a hardware integer and the induction variable 'i' could be kept in a cpu register. But with real arithmetic using bignums you can't assume that anymore and each iteration of the loop must do a relatively expensive comparison of 'i' against 'z'. In pseudo code: var z = x + y
var i = 0
if (!hw_int(z)) i = new_bignum(0)
while (true) {
// i < z
if (hw_ints?(i, z)) {
if (i >= z) break
} else {
if (bignum_gte(i, z)) break
}
... body ...
// i++
if (hw_int?(i)) {
i++;
} else {
i = bignum_add(i, 1)
}
}
|
|
If you know to use bigints or bignums, just type it and use their variants from the beginning.
Most better dynamic languages already use tagged ints, so they have only say ~30bits for an int and do a lot of those overflow checks and still beat the hell out of the poorly designed dynamic languages with full boxed ints or nums.