Chetan Haritas
Chetan Haritas

Reputation: 55

Which method is faster ob.setText(""+int) or ob.setText(String.valueOf(int);

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));

Upvotes: 2

Views: 113

Answers (2)

Cole Henrich
Cole Henrich

Reputation: 165

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.

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109597

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.

Upvotes: 3

Related Questions