Skip to content

Instantly share code, notes, and snippets.

@Falci
Forked from fernandogodoy/StringTest.java
Last active December 27, 2015 04:58
Show Gist options
  • Save Falci/7270290 to your computer and use it in GitHub Desktop.
Save Falci/7270290 to your computer and use it in GitHub Desktop.
@Test
public void testString()
String texto = "";
System.out.println("--------------------Operador + -----------------------------");
Date date = new Date(System.currentTimeMillis());
DateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
String initOperador = formatter.format(date);
System.out.println(initOperador);
for(int i = 0; i < 100000; i++){
texto += i + "text";
}
date = new Date(System.currentTimeMillis());
String fimOperador = formatter.format(date);
System.out.println(fimOperador);
BigDecimal a = new BigDecimal(initOperador.replaceAll(":", ""));
BigDecimal b = new BigDecimal(fimOperador.replaceAll(":", ""));
System.out.println("tempo : " + b.subtract(a) );
System.out.println("-------------------String Format----------------------------");
texto = "";
date = new Date(System.currentTimeMillis());
String initFormat = formatter.format(date);
System.out.println(initFormat);
for(int i = 0; i < 100000; i++){
texto = String.format("%s%dtext", texto, i);
}
date = new Date(System.currentTimeMillis());
String fimFormat = formatter.format(date);
System.out.println(fimFormat);
BigDecimal c = new BigDecimal(initFormat.replaceAll(":", ""));
BigDecimal d = new BigDecimal(fimFormat.replaceAll(":", ""));
System.out.println(String.format("tempo : %s", d.subtract(c)) );
System.out.println("-------------------String concat----------------------------");
texto = "";
date = new Date(System.currentTimeMillis());
String initConcat = formatter.format(date);
System.out.println(initConcat);
for(int i = 0; i < 100000; i++){
texto = texto.concat(String.valueOf(i)).concat("text");
}
date = new Date(System.currentTimeMillis());
String fimConcat = formatter.format(date);
System.out.println(fimConcat);
BigDecimal e = new BigDecimal(initConcat.replaceAll(":", ""));
BigDecimal f = new BigDecimal(fimConcat.replaceAll(":", ""));
System.out.println(new String("tempo : ").concat(f.subtract(e).toPlainString()));
System.out.println("-------------------String Builder---------------------------");
StringBuilder textoS = new StringBuilder();
date = new Date(System.currentTimeMillis());
String initStringBuilder = formatter.format(date);
System.out.println(initStringBuilder);
for(int i = 0; i < 100000; i++){
textoS.append(String.valueOf(i)).append("text");
}
date = new Date(System.currentTimeMillis());
String fimStringBuilder = formatter.format(date);
System.out.println(fimStringBuilder);
BigDecimal g = new BigDecimal(initStringBuilder.replaceAll(":", ""));
BigDecimal h = new BigDecimal(fimStringBuilder.replaceAll(":", ""));
System.out.println(new StringBuilder("tempo : ").append(h.subtract(g)));
System.out.println("-------------------String Buffer---------------------------");
StringBuffer textoSB = new StringBuffer();
date = new Date(System.currentTimeMillis());
String initStringBuffer = formatter.format(date);
System.out.println(initStringBuffer);
for(int i = 0; i < 100000; i++){
textoSB.append(String.valueOf(i)).append("text");
}
date = new Date(System.currentTimeMillis());
String fimStringBuffer = formatter.format(date);
System.out.println(fimStringBuffer);
BigDecimal i = new BigDecimal(initStringBuffer.replaceAll(":", ""));
BigDecimal j = new BigDecimal(fimStringBuffer.replaceAll(":", ""));
System.out.println(new StringBuffer("tempo: ").append(j.subtract(i)) );
}
//10x menos itens no loop
--------------------Operador + -----------------------------
17:13:27:330
17:14:07:718
tempo : 80388
-------------------String Format----------------------------
17:14:07:723
17:15:55:196
tempo : 147473
-------------------String concat----------------------------
17:15:55:198
17:16:46:805
tempo : 91607
-------------------String Builder---------------------------
17:16:46:806
17:16:46:819
tempo : 13
-------------------String Buffer---------------------------
17:16:46:820
17:16:46:854
tempo: 34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment