java - Decision of control mechanism (Necessity of if condition?) -


suppose have profile page has these values: name, last name, username, follower , following counts.

they may updated after user logged application. there refresh button. when user clicks refresh button, calling web service new values. in return, 5 values.

here example:

suppose have called web service , parsed return values response class.

// here first approach.  if (!response.name.equals(name)) {  name = response.name; }  if (!response.lastname.equals(lastname)) {  lastname = response.lastname; }   // here second.  name = response.name; lastname = response.lastname; 

in first approach, believe if condition required because if values same, won't lose time assign operation. , know if condition fastest operation can computer does.

in second approach, believe if condition not required because access both values in if condition (name , response.name), instead of losing time accessing them, not consider same or not. make assignment.

now, want know faster way?

thanks in advance.

the second approach preferable ... unless have made mistake of using '==' compare these strings elsewhere in code1.

the flaw in reasoning here:

"in first approach, believe if condition required because if values same, won't lose time assign operation."

in fact, assignment operator faster if test, ignoring cost of making string.equals call. assignment copies reference memory cell, , cheap2. not should think of "optimizing", unless you've got solid profiler evidence bottleneck.


1 - in case, there semantic difference between 2 approaches. chances neither approach going give right answer.

2 - in circumstances, assignment object field bit more expensive single memory write ... then, not should try optimize preemptively.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -