What is the fastest way to check that a String contains only alphanumeric characters.
I have a library for processing extremely large data files, that is CPU bound. I am explicitly looking for information about how to improve the performance of the alphanumeric checking process. I am wondering if there is a more efficient way to check than using pre-compiled regular expressions.
I’ve written the tests that compare using regular expressions (as per other answers) against not using regular expressions. Tests done on a quad core OSX10.8 machine running Java 1.6
Interestingly using regular expressions turns out to be about 5-10 times slower than manually iterating over a string. Furthermore the
isAlphanumeric2()function is marginally faster thanisAlphanumeric(). One supports the case where extended Unicode numbers are allowed, and the other is for when only standard ASCII numbers are allowed.