I am running Java software on an ARM v6 processor. The nature of this program requires me to convert some numbers (int or float) to String. The processor operates at a frequency of 850 MHz. Java Runtime - OpenJDK Zero VM 1.7.0_21-b02.
I don't expect awesome performances here, but I would expect something much more effective than what I see with the code snippet below.
long time1, time2;
float[] src = new float[2000000];
for (int i = 0; i < src.length; i++) {
src[i] = (float)Math.random()* 2.56454512f * (float) Math.random();
}
time1 = System.nanoTime();
for (int j = 0; j < src.length; j++) {
String test = String.valueOf(src[j]);
}
time2 = System.nanoTime();
logTimeDelay("String.valueOf", time1, time2);
time1 = System.nanoTime();
for (int j = 0; j < src.length; j++) {
String test = Float.toString(src[j]);
}
time2 = System.nanoTime();
logTimeDelay("Float.toString", time1, time2);
StringBuilder sb = new StringBuilder(50);
time1 = System.nanoTime();
for (int j = 0; j < src.length; j++) {
sb.setLength(0);
sb.append(src[j]);
}
time2 = System.nanoTime();
logTimeDelay("StringBuilder.append, setLength", time1, time2);
time1 = System.nanoTime();
for (int j = 0; j < src.length; j++) {
String test = "" + src[j];
}
time2 = System.nanoTime();
logTimeDelay("\"\" + ", time1, time2);
private static void logTimeDelay(String message, long time1, long time2){
System.out.println(String.format(message + ": %.5f s", (float) (time2 - time1) / 1.0e9));
}
Running this piece of code on my i7 computer returns the following results:
String.valueOf: 0.39714 s
Float.toString: 0.33295 s
StringBuilder.append, setLength: 0.33277 s
"" + : 0.37581 s
Running the same piece of code on an ARMv6 processor returns the following values:
String.valueOf: 204.78758 s
Float.toString: 200.79659 s
StringBuilder.append, setLength: 180.81551 s
"" + : 267.63036 s
Any tips on how I could optimize my numbers for converting int on this device?
Thanks in advance.