View RSS Feed

My Java Tips

Performance Issues (StringTokenizer)

Rate this Entry
by , 11-09-2011 at 04:46 PM (1618 Views)
StringTokenizer is a very useful class that is used by developers while parsing text. It works fine and produces the required results.

Java Code:
StringTokenizer st = new StringTokenizer(s,",");


StringTokenizer is very powerful and can handle large sets of delimiters at once. This makes it powerful but a little slow. What if we have a single character delimiter? Should we still use StringTokenizer? If we want to make life easy, the answer is yes. But if we are concerned about performance issues, the answer is no.

Using StringTokenizer to get tokens:

Java Code:
                String s= "Australia, Germany, Austria, France";
		String sub = null;
		StringTokenizer st = new StringTokenizer(s,",");

		try{
		while((sub = (String)st.nextToken()) != null)
		{
			// code goes here
			System.out.println(sub);
		}
		}catch(NoSuchElementException e)
		{

		}
Our own code to get tokens:

Java Code:
                String s= "Australia, Germany, Austria, France";
	        int i=0;
		int j = s.indexOf(",");
		while(j >= 0)
		{
			sub = s.substring(i,j);
			// code goes here
			i = j+1;
			j = s.indexOf(",", i);
		}
		sub = s.substring(i);
How we are finding the end of one token? We simply make a single call to indexOf method and get the end position of our token. The key point is that we know that our delimiter is of one character length so we coded accordingly. StringTokenizer on the other hand caters delimiter of variable length as well, so it calls indexOf() for every character in the string in order to decide if it belongs in the set of delimiters or not.

So the rule is, flexible functionality is powerful but slow whereas inflexible functionality is efficient.

Submit "Performance Issues (StringTokenizer)" to Facebook Submit "Performance Issues (StringTokenizer)" to Digg Submit "Performance Issues (StringTokenizer)" to del.icio.us Submit "Performance Issues (StringTokenizer)" to StumbleUpon Submit "Performance Issues (StringTokenizer)" to Google

Tags: None Add / Edit Tags
Categories
Performance

Comments