Skip to content

Instantly share code, notes, and snippets.

@mikeplate
Created December 8, 2010 15:34
Show Gist options
  • Save mikeplate/733416 to your computer and use it in GitHub Desktop.
Save mikeplate/733416 to your computer and use it in GitHub Desktop.
Speed test for integer to string conversion on Android
long start = SystemClock.uptimeMillis();
for (int i = 0; i < 1000; i++) {
String x = Integer.toString(i);
}
long total = SystemClock.uptimeMillis() - start;
Log.i("time", String.format("Total for toString is %d", total));
start = SystemClock.uptimeMillis();
for (int i = 0; i < 1000; i++) {
String x = String.valueOf(i);
}
total = SystemClock.uptimeMillis() - start;
Log.i("time", String.format("Total for valueOf is %d", total));
@mikeplate
Copy link
Author

Debugging this in the emulator with Android 2.1 got me the numbers 129 ms for toString and 162 ms for valueOf. Not much difference, but the difference speaks in toString's favor. Oh, and I removed the String.format("%d", i) sample but it took 51928 ms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment