Skip to content

Instantly share code, notes, and snippets.

@capablaza
Created January 18, 2016 03:01
Show Gist options
  • Save capablaza/5ca1252089abf9ff807d to your computer and use it in GitHub Desktop.
Save capablaza/5ca1252089abf9ff807d to your computer and use it in GitHub Desktop.
String Challenge
package cl.solojava.string.challenge.run;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AppRun {
public static final String DATE_FORMAT = "hh:mm:ss";
public static String DATA_PROOF = "adfghjklqrtyueiowmasbdabsod";
public static int LOOP_SIZE = 10000;
/***
* This proof allow know the real performance for all type String in Java
* language
*/
public static void main(String[] args) {
testStringInmutable();
testStringBuilder();
testStringBuffer();
}
private static void testStringBuffer() {
printLogRun("testStringBuffer");
timeCheckPoint();
StringBuffer varStringBuffer = new StringBuffer();
for (int i = 0; i < LOOP_SIZE; i++) {
varStringBuffer.append(DATA_PROOF);
}
timeCheckPoint();
}
private static void testStringBuilder() {
printLogRun("testStringBuilder");
timeCheckPoint();
StringBuilder varStringBuilder = new StringBuilder();
for (int i = 0; i < LOOP_SIZE; i++) {
varStringBuilder.append(DATA_PROOF);
}
timeCheckPoint();
}
private static void testStringInmutable() {
printLogRun("testStringInmutable");
timeCheckPoint();
String varString = "";
for (int i = 0; i < LOOP_SIZE; i++) {
varString = varString + DATA_PROOF;
}
timeCheckPoint();
}
public static void timeCheckPoint() {
long currentTime = System.currentTimeMillis();
Date currentDate = buildDate(currentTime);
System.out.println(dateToString(currentDate));
}
public static Date buildDate(long time) {
return new Date(time);
}
public static String dateToString(Date dateToConvert) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(AppRun.DATE_FORMAT);
return simpleDateFormat.format(dateToConvert);
}
public static void printLogRun(String testLog) {
System.out.println("Running [" + testLog + "] ...");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment