Test results

This page contains the results of the performance test provided with the application.

Test results

Method Result under Windows (ms)
testString 1732
testStringBuffer 130
objNotReused 160
objReused 190
v_vs_l_Vector 250
v_vs_l_List 210

Test configuration: loopcount=10000
HARDWARE=AMD Duron 900Mhz, 256 SDRAM
OP.SYS=Windows XP, SUSE Linux 7.3+ Kernel: 2.4.20 KDE2
(I got weird values under Linux, thus I haven't put the result in the table.)

Look at the results and decide which method you use in your application. The reason of the high difference between string concatenation with the += operator and the append method of the StringBuffer class is following:
Each time if you use the += operator the append method will be invoked by the Java VM. So if there is a lot of string concatenation in your program, use StringBuffer and append() method.

Source code of the tester class

import java.util.*;
public class PerfTest{
	////string concatenation
	private static void testString(int n){
		String s="";
		for(int i=0;i<n;i++)	{
			s+="a";
		}
		System.out.println("test 1 ready");
	}
	
	private static void testStringBuffer(int n){
		StringBuffer sb=new StringBuffer("");
		for(int i=0;i<n;i++)	{
			sb.append("a");
		}
		sb.toString();
		System.out.println("test 2 ready");
	}
	
	////reusing objects
	private static void objNotReused (int n){
		for(int i=0;i<n;i++)	{
			String s="string";
			Integer j=new Integer(i);
			Double d=new Double(16.66666);
		}
		System.out.println("test 3 ready");
	}
	
	private static void objReused(int n){
		String s;
		Integer j;
		Double d;
		for(int i=0;i<n;i++)	{
			s="string";
			j=new Integer(i);
			d=new Double(16.66666);
		}
		System.out.println("test 4 ready");
	}

	
	////Vector vs list
	private static void v_vs_l_Vector(int n){
		Vector v=new Vector(n/2,10);
		Integer j;
		for(int i=0;i<n;i++)	{
			j=new Integer(i);
			v.add(j);
			v.get(i);
		}
		v.size();
		System.out.println("test 5 ready");
	}

	private static void v_vs_l_List(int n){
		List l=new Vector(n/2,10);
		Integer j;
		for(int i=0;i<n;i++)	{
			j=new Integer(i);
			l.add(j);
			l.get(i);
		}
		l.size();
		System.out.println("test 6 ready");
	}
	
	private static void runTests(int n){
		System.out.println("\n\n**********************************************");
		System.out.println(" test started ");
		System.out.println(" loop count: "+n);
		System.out.println("\n\nIt will take at about: "+estTime(n)+"s");	
		System.out.println("**********************************************");
		testString(n);
		testStringBuffer(n);
		objNotReused(n);
		objReused(n);
		v_vs_l_List(n);
		v_vs_l_Vector(n);
	}
	
	private static int estTime(int n){
		n*=230;
		long t = System.currentTimeMillis();
		for(int i=0;i<n;i++);
		long dt=System.currentTimeMillis()-t;
		return (int)dt;
	}
	
	
	public static void main(String args[]){
		if(args.length!=1){
			runTests(10000);			
		}else{
			try{
				int n=Integer.parseInt(args[0]);
				runTests(n);
			}catch (NumberFormatException ex){
				System.out.println("Use: java PerfTest [int value]");
				System.out.println("java PerfTest 10000");
				System.out.println("java PerfTest without arguments means, 
				that the default loop count will be used.");
				System.out.println("default loop count: 10000");
			}
		}
	}


}