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());
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
.
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.
[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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With