The Daily WTF
Frequently in programming, we can make a tradeoff: use less (or more) CPU in exchange for using more (or less) memory. Lookup tables are a great example: use a big pile of memory to turn complicated calculations into O(1) operations.
So, for example, implementing itoa, the C library function for turning an integer into a character array (aka, a string), you could maybe make it more efficient using a lookup table.
I say “maybe”, because Helen inherited some C code that, well, even if it were more efficient, it doesn’t help because it’s wrong.
Let’s start with the lookup table:
char an[1000][3] = { {‘0′,’0′,’0’},{‘0′,’0′,’1’},{‘0′,’0′,’2’},{‘0′,’0′,’3’},{‘0′,’0′,’4’},{‘0′,’0′,’5’},{‘0′,’0′,’6’},{‘0′,’0′,’7’},{‘0′,’0′,’8’},{‘0′,’0′,’9’}, {‘0′,’1′,’0’},{‘0′,’1′,’1’},{‘0′,’1′,’2’},{‘0′,’1′,’3’},{‘0′,’1′,’4’},{‘0′,’1′,’5’},{‘0′,’1′,’6’},{‘0′,’1′,’7’},{‘0′,’1′,’8’},{‘0′,’1′,’9’}, …
I’m abbreviating the lookup table for now. This lookup table is meant to be use to convert every number from 0…999 into a string representation.
Let’s take a look at how it’s used.
int ll = f->cfg.len_len; long dl = f->data_len; //
To read the full article click on the 'post' link at the top.