I found this in some code that I wanted to optimize. Here is a sniper:
tempString = bigBuffer.replaceAll("\\n", "");
tempString = tempString.replaceAll("\\t", "");
Then I decided to use regex wisely, and I did this:
tempString = bigBuffer.replaceAll("[\\n\\t]", "");
Then a friend told me to do this instead:
tempString = bigBuffer.replaceAll("\\n|\\t", "");
Since I like to know the results of my changes, I did a test to check if it was a good optimization. So the result with (java version "1.6.0_27") is that the first code is a 100% link.
With a pipe - 121%, so it took more time to complete the task.
With a square bracket, it is 52%, so it took less time to complete the task.
Why does a regular expression behave differently, where should it be the same?
Martin