//How to check String ,StringBuffer and StringBuilder classes performances?
public class StringStringBufferStringBuillderPerformance {
public static void main(String[] args) {
long stime=System.currentTimeMillis();
String s1="govind";
for(int i=0;i<=10000;i++)
{
s1=s1.concat("khan");
}
System.out.println("String takes time="+(System.currentTimeMillis()-stime)+"ms");
long sbtime=System.currentTimeMillis();
StringBuffer sb=new StringBuffer("govind");
for(int i=0;i<=10000;i++)
{
sb.append("khan");
}
System.out.println("stringBuffer takes time="+(System.currentTimeMillis()-sbtime+"ms"));
long sbiltime=System.currentTimeMillis();
StringBuilder sb1=new StringBuilder("govind");
for(int i=0;i<=10000;i++)
{
sb1.append("khan");
}
System.out.println("stringBuilder takes time="+(System.currentTimeMillis()-sbiltime+"ms"));
}
}
public class StringStringBufferStringBuillderPerformance {
public static void main(String[] args) {
long stime=System.currentTimeMillis();
String s1="govind";
for(int i=0;i<=10000;i++)
{
s1=s1.concat("khan");
}
System.out.println("String takes time="+(System.currentTimeMillis()-stime)+"ms");
long sbtime=System.currentTimeMillis();
StringBuffer sb=new StringBuffer("govind");
for(int i=0;i<=10000;i++)
{
sb.append("khan");
}
System.out.println("stringBuffer takes time="+(System.currentTimeMillis()-sbtime+"ms"));
long sbiltime=System.currentTimeMillis();
StringBuilder sb1=new StringBuilder("govind");
for(int i=0;i<=10000;i++)
{
sb1.append("khan");
}
System.out.println("stringBuilder takes time="+(System.currentTimeMillis()-sbiltime+"ms"));
}
}
o/p:-
String takes time=426ms
stringBuffer takes time=7ms
stringBuilder takes time=1ms
No comments:
Post a Comment