Today’s article is quick and to the point: when you need to take the base 10 logarithm of an integer you can speed this up by about 8x. Read on for the technique and save some CPU cycles!
Archive for August, 2012
Many programmers are aware of a special case where you can use a bitwise shift for multiplication or division when you’re multiplying or dividing by a power of two. For example, you can replace
i * 2 with
i < < 1 and
i / 2 with
i >> 1. A lesser-known trick works for modulus. But are these alternatives actually any faster? Today’s article puts them to the test!
Math.abs is a commonly-used utility function for taking the absolute value of a
Number. However, there’s no special-case version for taking the absolute value of an
int. Of course
Math.abs will work for
int values, but we can do it faster. Read on for a couple of ways.