Created
December 8, 2010 15:34
-
-
Save mikeplate/733416 to your computer and use it in GitHub Desktop.
Speed test for integer to string conversion on Android
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
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.