Last active
August 29, 2015 14:15
-
-
Save AGBrown/48b25ec4fee0a5884383 to your computer and use it in GitHub Desktop.
Caliper benchmark code for int[] -> String in Java (ref: http://stackoverflow.com/a/28611711/1945631 )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package text; | |
/** Converter that uses bit-shifting to lookup each nibble in a fixed hex | |
* encoding array, yielding higher performance | |
* @author Andrew Brown | |
*/ | |
public class AltConverter implements IntArrToStringConverter { | |
final protected static char[] encoding = "0123456789ABCDEF".toCharArray(); | |
public String convertToString(int[] arr) { | |
char[] encodedChars = new char[arr.length * 4 * 2]; | |
for (int i = 0; i < arr.length; i++) { | |
int v = arr[i]; | |
int idx = i * 4 * 2; | |
for (int j = 0; j < 8; j++) { | |
encodedChars[idx + j] = encoding[(v >>> ((7-j)*4)) & 0x0F]; | |
} | |
} | |
return new String(encodedChars); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package text; | |
/** A standard interface for the converter to allow a standardised caliper test | |
* @author Andrew Brown | |
*/ | |
public interface IntArrToStringConverter { | |
/** Converts an int[] to a hex String, 0-padding the entire int from each element | |
* of the array | |
* @param arr the array to convert | |
* @return A hex-string (base-16) version of the array | |
*/ | |
public abstract String convertToString(int[] arr); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package text; | |
/** Simple converter that corrects the original OP's implementation | |
* @author Malt | |
* @author Andrew Brown | |
*/ | |
public class SimpleConverter implements IntArrToStringConverter { | |
public String convertToString(int[] arr) { | |
StringBuilder builder = new StringBuilder(arr.length * 8); | |
for (int b : arr) { | |
builder.append(byteToUnsignedHex(b)); | |
} | |
return builder.toString(); | |
} | |
public static String byteToUnsignedHex(int i) { | |
String hex = Integer.toHexString(i); | |
while(hex.length() < 8){ | |
hex = "0" + hex; | |
} | |
return hex; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package text; | |
import java.util.Arrays; | |
import com.google.caliper.*; | |
/** Google caliper benchmarking class for the @link IntArrToStringConverter | |
* classes | |
* @author Andrew Brown | |
*/ | |
public class TextCaliper { | |
@Param({"1", "100", "1000", "100000", "100000000"}) int N; | |
@Param({"text.SimpleConverter", "text.AltConverter"}) ConverterFactory sutFactory; | |
private int[] array; | |
@BeforeExperiment void setUp() { | |
array = new int[N]; | |
} | |
@Benchmark int test(int reps) { | |
int dummy = 0; | |
for (int i = 0; i < reps; i++) { | |
String out = sutFactory.getConverter().convertToString(array); | |
dummy |= out.length(); | |
} | |
return dummy; | |
} | |
/** Factory class to enable parameterisation of the concrete implementation | |
* test target classes | |
* @author Andrew Brown | |
*/ | |
public static class ConverterFactory{ | |
private IntArrToStringConverter converter; | |
public IntArrToStringConverter getConverter() { | |
return converter; | |
} | |
public ConverterFactory(IntArrToStringConverter converter) { | |
this.converter = converter; | |
} | |
public static ConverterFactory valueOf(String className) { | |
Object converter; | |
try { | |
converter = Class.forName(className).newInstance(); | |
} catch (InstantiationException | IllegalAccessException | |
| ClassNotFoundException e) { | |
throw new IllegalArgumentException("className", e); | |
} | |
return new ConverterFactory((IntArrToStringConverter)converter); | |
} | |
@Override | |
public String toString() { | |
return converter.getClass().getName(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment