MAC
MAC

Reputation: 15847

Which is optimized way?

May be simple question but i am confused

which code is optimized ? and should i use ?

what is the difference in internal process ?

String str = editText.getText().toString();     
str =str.trim().toLowerCase();
textView.setText(str);

textView.setText(editText.getText().toString().trim().toLowerCase());

Upvotes: 6

Views: 385

Answers (6)

Dalmas
Dalmas

Reputation: 26547

Don't think that if you put everything on a single line, it will be better than if you split the statement in multiple lines. Generally the Java compiler is smart enough to produce exactly the same bytecode in both cases. Modern compilers do a lot of micro-optimizations.

You can check if there's a difference by compiling them, then decompile the bytecode with the command javap -c.

Edit :

I just tested and here are the results :

String str = editText.getText().toString();     
str = str.trim().toLowerCase();
textView.setText(str);

compiles to :

       0: aload_0       
       1: getfield      #7                  // Field textView:Landroid/widget/TextView;
       4: aload_0       
       5: getfield      #4                  // Field editText:Landroid/widget/EditText;
       8: invokevirtual #8                  // Method android/widget/EditText.getText:()Landroid/text/Editable;
      11: invokevirtual #9                  // Method java/lang/Object.toString:()Ljava/lang/String;
      14: invokevirtual #10                 // Method java/lang/String.trim:()Ljava/lang/String;
      17: invokevirtual #11                 // Method java/lang/String.toLowerCase:()Ljava/lang/String;
      20: invokevirtual #12                 // Method android/widget/TextView.setText:(Ljava/lang/CharSequence;)V
      23: return        

and the second one :

textView.setText(editText.getText().toString().trim().toLowerCase());

gives the following result :

       0: aload_0       
       1: getfield      #7                  // Field textView:Landroid/widget/TextView;
       4: aload_0       
       5: getfield      #4                  // Field editText:Landroid/widget/EditText;
       8: invokevirtual #8                  // Method android/widget/EditText.getText:()Landroid/text/Editable;
      11: invokevirtual #9                  // Method java/lang/Object.toString:()Ljava/lang/String;
      14: invokevirtual #10                 // Method java/lang/String.trim:()Ljava/lang/String;
      17: invokevirtual #11                 // Method java/lang/String.toLowerCase:()Ljava/lang/String;
      20: invokevirtual #12                 // Method android/widget/TextView.setText:(Ljava/lang/CharSequence;)V
      23: return        

As you can see I guessed right, they are identical. The java compiler optimized the first example and completely removed the variable as it was useless.

So the conclusion is that you should use the code that you find the more readable.

Upvotes: 4

Hardik Nadiyapara
Hardik Nadiyapara

Reputation: 2436

in first one you use additional variable it use more memory than second one. as far as second has a advantage of memory efficiency

Upvotes: 1

Chirag
Chirag

Reputation: 56925

Here first you store your output in String variable so it occupy space for that then after you convert it into lowercase and set into textview.

And in second option you set into textview without storing it into any varible.

So second option is preferable if you do not want to use it in further coding.

Upvotes: 1

Ahmad
Ahmad

Reputation: 2190

Well, the second is less readable though it has advantage of more memory efficiency. Not assigning objects a reference variable makes them more eligible for Garbage Collection.

But needles to say, you should prefer readability over such small optimizations.

Upvotes: -1

Chintan Raghwani
Chintan Raghwani

Reputation: 3370

[1] creating String str occupies memory of the device, But it can be used later also; So if you need it later then it is optimized.

[2] that doesn't use memory, so simply optimized, But you need this string later then you have to fetch it all time, so it takes more machine-cycles to complete the process, in that case, 2nd one is lesser optimized.

Upvotes: 2

codetiger
codetiger

Reputation: 2779

In the first you are using an additional variable, it takes more memory than the second one.

Upvotes: -2

Related Questions