Skip to content

Instantly share code, notes, and snippets.

@fernandogodoy
Last active December 11, 2019 01:55
Show Gist options
  • Save fernandogodoy/7268476 to your computer and use it in GitHub Desktop.
Save fernandogodoy/7268476 to your computer and use it in GitHub Desktop.
@Test
public void testString(){
System.out.println("--------------------Operador + -----------------------------");
String textoOperador = "";
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++){
textoOperador = textoOperador + "text" + 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----------------------------");
String textoFormat = "";
date = new Date(System.currentTimeMillis());
String initFormat = formatter.format(date);
System.out.println(initFormat);
for(int i = 0; i < 100000; i++){
textoFormat = String.format("%s text %s text", textoFormat, 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----------------------------");
String textoConcat = "";
date = new Date(System.currentTimeMillis());
String initConcat = formatter.format(date);
System.out.println(initConcat);
for(int i = 0; i < 100000; i++){
textoConcat = new String().concat(textoConcat).concat("text").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 textoBuilder = new StringBuilder();
date = new Date(System.currentTimeMillis());
String initStringBuilder = formatter.format(date);
System.out.println(initStringBuilder);
for(int i = 0; i < 100000; i++){
textoBuilder = textoBuilder.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 textoBuffer = new StringBuffer();
date = new Date(System.currentTimeMillis());
String initStringBuffer = formatter.format(date);
System.out.println(initStringBuffer);
for(int i = 0; i < 100000; i++){
textoBuffer = textoBuffer.append("text").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)) );
}
--------------------Operador + -----------------------------
16:57:38:651
16:58:41:453
tempo : 102802
-------------------String Format----------------------------
16:58:41:455
17:00:45:106
tempo : 4203651
-------------------String concat----------------------------
17:00:45:106
17:02:30:415
tempo : 185309
-------------------String Builder---------------------------
17:02:30:416
17:02:30:444
tempo : 28
-------------------String Buffer---------------------------
17:02:30:444
17:02:30:483
tempo: 39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment