What is optimized?

Maybe a simple question, but I'm confused

which code is optimized? and should i use?

What is the difference in the internal process?

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

 textView.setText(editText.getText().toString().trim().toLowerCase()); 
+6
source share
6 answers

Do not think that if you put everything on one line, it will be better than if you split the statement into several lines. Typically, the Java compiler is smart enough to create the same bytecode in both cases. Modern compilers do a lot of micro-optimizations.

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

Edit:

I just tested and here are the results:

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

compiles:

  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 second:

 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 understood correctly that they are identical. The java compiler optimized the first example and completely deleted the variable, since it was useless.

So, conclude that you should use code that you find more readable.

+4
source

[1] creation String str takes up the device’s memory, but it can be used later ; Therefore, if you need it later, it is optimized.

[2] which does not use memory , therefore it is simply optimized . But you will need this line later , you should always load it , so it takes more machine cycles to complete the process , in this case the second option is less optimized.

+2
source

Here, first you save your output in a String variable to take up space for this, after you convert it to lowercase and TextView.

And in the second option, you install in the text box without saving it in any varible.

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

+1
source

In the first case, you use an additional variable that uses more memory than the second. as far as the second has the advantage of memory efficiency

+1
source

Well, the second is less readable, although it has the advantage of greater memory efficiency. Without assigning objects, the reference variable makes them more suitable for garbage collection.

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

-1
source

In the first you use an additional variable, it takes more memory than the second.

-2
source

Source: https://habr.com/ru/post/921554/


All Articles