3 ms·
I was noodling in this area recently, trying to speed up some code similar to the tr utility: $ echo abcdef |tr abc ghi ghidef For an eight-bit charac
by SloopJon 2y ago
I was noodling in this area recently, trying to speed up some code similar to the tr utility:
$ echo abcdef |tr abc ghi
ghidef
For an eight-bit character set, I found that building an array to map every character improved on linear search, even for short replacement strings and relatively short input strings.
There isn't as easy a win for Unicode, so I played with some simple hash tables. Although the conventional wisdom is to use the high-order bits of a hash function's output, FNV-1a is not so good for short inputs of one or two bytes. (I used the 32-bit variant.) It was better just to use the low-order bits.