3 ms·
> Integer addition arithmetic is one of the most common operations your computer does, making that 2-10x or more slower would have catastrophic consequences. C
by ScottBurson 11y ago
> Integer addition arithmetic is one of the most common operations your computer does, making that 2-10x or more slower would have catastrophic consequences.
Common Lisp is a compiled language that has arbitrary-precision integer arithmetic by default. What we generally find is that in most kinds of code the cost of the conditional branch is negligible; it's dwarfed by memory access, function call, and other conditional logic in the program.
There are, of course, exceptions. Most of these are array operations where one is doing a small amount of work on each of a large number of array elements; array index computations are a good example of a place where one doesn't generally want to pay the cost of overflow checking. For those situations, Common Lisp provides declarations one can write to cause the overflow checking to be skipped.
The overall effect is far from catastrophic. Common Lisp is competitive with other compiled languages -- not the very fastest, perhaps, but definitely in that range, and a good 10 to 30 times faster than interpreted Python or Ruby -- languages in which a lot of code is being written these days.
Now to this specific example. What would have happened if it were written in CL? If the programmer had not specified otherwise, the arithmetic would be done in arbitrary precision and would work correctly. That's clearly the correct choice in this case: even though an array index is being computed, it's effectively a random index from the hardware's point of view; cache prefetching will be no help at all. That being the case, the performance will be dominated by memory latency (at least to the L2 and L3 caches, if not all the way to main memory). Also, in many applications, the comparison between two elements will involve a function call; it won't be just an integer less-than instruction. So worrying about a couple of integer overflow tests would be pretty silly.
I concede the possibility that, this argument notwithstanding, the programmer might have turned off overflow checking anyway. Well, you can shoot yourself in the foot in any language. Lisp just tries to make it not be the default.