String manipulation is one of the most commonly used aspect of programming. We do a lot of string manipulation like replacing a particular word in a string, stripping the string of unwanted words/tags, concatenating a string etc., .
It is very easy to introduce inefficiencies in the code while doing string manipulation. Programming languages provides a lot of built in API’s to manipuate a string. There is a very little chance for the built in API(s) to have inefficient code. Mostly it is the incorrect usage of API’s by us which brings in the inefficiency.
Let us take a case to illustrate this point,
Do a HTML escape for the given html document. The code should convert all the <, > , & , ‘ , ” in the string to < , > , & , ' , " respectively.
Source: Codeexperiments
Version 1:
This can be easily accomplished by using the java api String.replaceAll(String regex, String replacement). The following code does the html escaping.
String str = “
”;
Hello World
str = str.replaceAll(“<”,”<”); str = str.replaceAll(“>”,”>”);
str = str.replaceAll(“\”",”"”);
str = str.replaceAll(“\’”,”'”);
str = str.replaceAll(“\’”,”&”);
As you can see the code is very simple and clean. Does the job of HTML escaping the given string. But is it efficient?
Version 2:
We shall try to do HTML escaping without using the String.replaceAll() method. The following code analyzes each character in the given string and replaces it with the appropriate escaped value.
StringBuilder strBuilder = new StringBuilder();
int size = strIn.length();
for(int i = 0 ; i < charval =" strIn.charAt(i);">’:
{
strBuilder.append(“>”);
break;
}
case ‘\”:
{
strBuilder.append(“'”);
break;
}
case ‘\”‘:
{
strBuilder.append(“"”);
break;
}
case ‘&’:
{
strBuilder.append(“&”);
break;
}
default:
{
strBuilder.append(charVal);
}
}
}String escapedString = strBuilder.toString();
Results:
Each version of the code was run 1000 times in a loop and here are the results
Version 1: Time taken for String.replaceAll(str,str) ::250 ms
Version 2: Time taken for custom replace all code ::15 ms
On running both the version of the code, it is found the version 2 which did not use the String.replaceAll() was on an average 10 to 15x faster than the version 1 of the code which used String.replaceAll().
No comments:
Post a Comment