2

Want to know which method is faster in android. Just for knowledge.

TextView t;
t = (TextView) findViewById(R.id.TextView_ID);

int number=5;
t.setText(""+number);

or

t.setText(String.valueOf(number));
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • Try it out! You can write a for loop that runs a million times and run a timer to see how long it takes to do each operation a million times. A million sounds like a lot, but it should take less than 5 seconds. – Cole Henrich Jan 13 '21 at 18:19
  • there's evidence now as to why t.setText(String.valueOf(number)) is faster. – Cole Henrich Jan 13 '21 at 20:01
  • Check out why it is faster: https://stackoverflow.com/a/65709549/14921272 – Cole Henrich Jan 13 '21 at 20:50

2 Answers2

3

String.valueOf(int) is fastest, as most direct. However this is a micro-optimisation. The compiler probably does optimize this itself.

"" + number would do a conversion too, and a string concatenation. Theoretically.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

t.setText(String.valueOf(number)) is measurably faster. Here is proof from running a simple benchmark where each of the methods, t.setText(""+number) and t.setText(String.valueOf(number)) were run 100 million times using Math.random() * 10000 as number. This process was repeated 3 times over so 2 methods, run 100 million times each, times 3, is 600 million repetitions - hopefully convincing as to its trustworthiness.

100000000 iterations:
4265705942 nanoseconds
^^empty string + number^^
4405272846 nanoseconds
^^String.valueOf()^^
t.setText(String.valueOf(number)) takes 1.0327184 the time of t.setText(""+number).
t.setText(String.valueOf(number)) is -139.56711 milliseconds faster than t.setText(""+number)
100000000 iterations:
4222272915 nanoseconds
^^empty string + number^^
4082684488 nanoseconds
^^String.valueOf()^^
t.setText(String.valueOf(number)) takes 0.9669399 the time of t.setText(""+number).
t.setText(String.valueOf(number)) is 139.58861 milliseconds faster than t.setText(""+number)
100000000 iterations:
4139763946 nanoseconds
^^empty string + number^^
4084830689 nanoseconds
^^String.valueOf()^^
t.setText(String.valueOf(number)) takes 0.98673034 the time of t.setText(""+number).
t.setText(String.valueOf(number)) is 54.933247 milliseconds faster than t.setText(""+number)

As you can see, t.setText(String.valueOf(number)) ran faster by 54.933247 milliseconds once and 139.58861 milliseconds another. Meanwhile, t.setText(""+number) ran 139.56711 milliseconds faster in one trial. As you can see, they are comparable - the major influencing factor that caused their oscillation was the use of a pseudorandom number. As you can see below, t.setText(String.valueOf(number)) is faster in all three trials when the number is set to 1.

100000000 iterations:
1292082781 nanoseconds
^^empty string + number^^
922339178 nanoseconds
^^String.valueOf()^^
t.setText(String.valueOf(number)) takes 0.71383905 the time of t.setText(""+number).
t.setText(String.valueOf(number)) is 369.74362 milliseconds faster than t.setText(""+number)
100000000 iterations:
835123611 nanoseconds
^^empty string + number^^
801558559 nanoseconds
^^String.valueOf()^^
t.setText(String.valueOf(number)) takes 0.9598083 the time of t.setText(""+number).
t.setText(String.valueOf(number)) is 33.565056 milliseconds faster than t.setText(""+number)
100000000 iterations:
824446908 nanoseconds
^^empty string + number^^
776221856 nanoseconds
^^String.valueOf()^^
t.setText(String.valueOf(number)) takes 0.94150615 the time of t.setText(""+number).
t.setText(String.valueOf(number)) is 48.225086 milliseconds faster than t.setText(""+number) 

In sum, t.setText(String.valueOf(number)) is slightly faster than t.setText(""+number).

Thanks to SeanF, here is an explanation of why it's faster:

String addition results in the compiler creating a StringBuilder instance, followed by append calls for each added element, followed by a call to StringBuilder.toString() to create the resulting concatenated String instance. So, ""+number creates a StringBuilder, appends a number using the same conversion as String.valueOf, and then creates a String instance from the StringBuilder with StringBuilder.toString. String.valueOf(number) avoids the StringBuilder, just using the value from String.valueOf.

Cole Henrich
  • 155
  • 3
  • 17