String Benchmark

Ao ler o post do Chris Nokleberg's sobre Switching on Strings resolvi fazer um bechmark de compraração de String entre equals e swith.



Eis os códigos:

package org.diegopacheco.stringbenchmark.datasource;
public class StringFactory{

private static String base = "ac";
public static String build(){

String ret = "";
if ((Math.random()*100)<=50.0){
ret = base + "x";
}else{
ret = base + "y";
}
return ret;      
}   
}
package org.diegopacheco.stringbenchmark.interfaces;

public interface Compare {
public void compare(String str);
}
package org.diegopacheco.stringbenchmark.strequals;

import org.diegopacheco.stringbenchmark.interfaces.Compare;

public class StringEqualsCompare implements Compare {

public void compare(String str){
String x = "";
if ("acx".equals(str)){
x = "acx";
}else if ("acy".equals(str)){
x = "acy";
}else{
x = "noth";
}
x = 10 + "";
//System.out.println(x);
}

}
package org.diegopacheco.stringbenchmark.swithstring;

import org.diegopacheco.stringbenchmark.interfaces.Compare;

public class SwithStringCompare implements Compare {

public void compare(String str) {
String x = "";
switch (str.length()) {
case 3:
switch(str.charAt(0)){
case 'a':
{
switch(str.charAt(1)){
case 'c':
{
switch(str.charAt(2)){
case 'x':
x = "acx";
break;
case 'y':
x = "acy";
break;       
}      
}
}
}
}
break;

default:
x = "noth";
break;
}
x = 10 + "";
//System.out.println(x);
}

}
package org.diegopacheco.stringbenchmark.test;

import java.util.ArrayList;
import java.util.List;

import org.diegopacheco.stringbenchmark.datasource.StringFactory;
import org.diegopacheco.stringbenchmark.interfaces.Compare;
import org.diegopacheco.stringbenchmark.strequals.StringEqualsCompare;
import org.diegopacheco.stringbenchmark.swithstring.SwithStringCompare;

public class TestCompare {

public static void main(String[] args) {

int totComparations = 1000000;

Compare compEquals = new StringEqualsCompare();
Compare compSwitch = new SwithStringCompare();

long timeEquals = 0L;
long timeSwitch = 0L;

List strs = new ArrayList();
for(int i=0;i<=totComparations;i++){    strs.add(StringFactory.build());   }      for(String s: strs){    timeSwitch += doCompare(compSwitch,s);    timeEquals += doCompare(compEquals,s);      }      System.out.println("Equals Compare Time  : " + timeEquals);   System.out.println("Switch Compare Time  : " + timeSwitch);    }   private static long doCompare(Compare comp,String s) {   long init = System.currentTimeMillis();   comp.compare(s);     long end = System.currentTimeMillis();   return end - init;  }   } 
** Os resultados variam mas a técnica de switch apesar do código ser maior ela é mais rápida. Um dos resultados foi: Equals Compare Time : 270 Switch Compare Time : 175 Mas isso é quase insignificante. Só se justificaria usar isso em casos de extrema performance. É isso ai Cães Stygios. :)

Popular posts from this blog

Telemetry and Microservices part2

Installing and Running ntop 2 on Amazon Linux OS

Fun with Apache Kafka