Wednesday, May 25, 2011

Configuring Tomcat in Eclipse

Remove all the web applications already deployed into the server using

right click on server in servers tab -> Add or Remove

Then publish it to the server

right click on server -> publish

Ten doublt click on the server. Server locations tab is enabled now. You can edit there....

Thursday, April 28, 2011

Comparing StringBuffer , StringBuilder n normal String

As of release JDK 5, StringBuffer class has been supplemented with an equivalent class designed for use by a single thread, {StringBuilder}. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization

Just run the following code for String concatenation..

public class ConcatPerf {
private static final int ITERATIONS = 100000;
private static final int BUFFSIZE = 16;

private void concatStrAdd() {
System.out.print("concatStrAdd -> ");
long startTime = System.currentTimeMillis();
String concat = "";
for (int i = 0; i < ITERATIONS; i++) {
concat += i % 10;
}
long endTime = System.currentTimeMillis();
System.out.print("length: " + concat.length());
System.out.println(" time: " + (endTime - startTime));
}

private void concatStrBuff() {
System.out.print("concatStrBuff -> ");
long startTime = System.currentTimeMillis();
StringBuffer concat = new StringBuffer(BUFFSIZE);
for (int i = 0; i < ITERATIONS; i++) {
concat.append(i % 10);
}
long endTime = System.currentTimeMillis();
System.out.print("length: " + concat.length());
System.out.println(" time: " + (endTime - startTime));
}

private void concatStrBuild() {
System.out.print("concatStrBuild -> ");
long startTime = System.currentTimeMillis();
StringBuilder concat = new StringBuilder(BUFFSIZE);
for (int i = 0; i < ITERATIONS; i++) {
concat.append(i % 10);
}
long endTime = System.currentTimeMillis();
System.out.print("length: " + concat.length());
System.out.println(" time: " + (endTime - startTime));
}

public static void main(String[] args) {
ConcatPerf st = new ConcatPerf();
System.out.println("Iterations: " + ITERATIONS);
System.out.println("Buffer : " + BUFFSIZE);

st.concatStrBuff();
st.concatStrBuild();
st.concatStrAdd();
}
}


Iterations: 100000
Capacity : 16
concatStrBuff -> length: 100000 time: 16
concatStrBuild -> length: 100000 time: 15
concatStrAdd -> length: 100000 time: 10437

This makes it clear I should never use plain String concatenation in big loops. Of course if you just concatenate a few strings once in a while this doesn’t matter.


Increasing the initial capacity for StringBuffer and StringBuilder doesn’t make much difference. Clearly with only 100,000 iterations the numbers for StringBuffer and StringBuilder are just noise.


Iterations: 100000
Capacity : 100000
concatStrBuff -> length: 100000 time: 15
concatStrBuild -> length: 100000 time: 16
concatStrAdd -> length: 100000 time: 10594

Let’s crank it up… but without the String concatenation test because it will never finish.


Iterations: 100000000
Capacity : 16
concatStrBuff -> length: 100000000 time: 15142
concatStrBuild -> length: 100000000 time: 10891

Now it is pretty clear StringBuilder is much faster because it avoids synchronization.


Iterations: 100000000
Capacity : 100000000
concatStrBuff -> length: 100000000 time: 14220
concatStrBuild -> length: 100000000 time: 10611


So StringBuilder is faster by a good percentage but remember that it is not thread safe.

Thanks : littletutorials.com