开发者

Which code's perfomance is better?

开发者 https://www.devze.com 2023-03-25 03:30 出处:网络
I have two same conditions and i want to know which is well in perfomance ? if(str_final.charAt(str_final.length() -1) == \'a\'

I have two same conditions and i want to know which is well in perfomance ?

if(str_final.charAt(str_final.length() -1) == 'a'
    || str_final.charAt(str_final.length() -1) == 'b' )
{
    // body 
}

--------------------OR--------------------

char te开发者_C百科mp = str_final.charAt(str_final.length() -1);
if( temp == 'a' || temp == 'b')
{
    // body
}


The second version is a tiny little bit faster.

Reason:
2 method calls less (if it doesn't end with 'a').
But both do not much more than return variable;:

  • charAt() is only an access to a array, and
  • the length() of the string is already pre-computed as well.


Second code is better option as str_final.charAt(str_final.length() -1) is just performed once in worst case also.

char temp = str_final.charAt(str_final.length() -1);

if( temp == 'a' || temp == 'b')
{
  // body
}

In the other case str_final.charAt(str_final.length() -1) has to be performed twice.


It might be the same performance, as an optimizing compiler (or just in time compiler/hotspot) could change the 1st version to the 2nd at compile time.


I would go for the second option because it is easier to read. Also str_final.length() and str_final.charAt() are only called once.

In addition, instead of calling the char temp, call it something more meaningful like lastChar.

You might even consider using a switch statement if you have more than just two characters to check:

char lastChar = str_final.charAt(str_final.length() -1);
switch (lastChar) {
  case 'a':
  case 'b':
    //do something
    break;
  case 'c':
    //something else
    break;
  case 'd':
    //something else
    break;
}


Second option would be faster and also much more readable, however better to use some other name than 'temp'


The second piece of code is faster because .charAt and .length() are evaluated only once. As others pointed out, readability is clearly more important though, which is also favouring the second solution.


Is this a catch question? I would certainly prefer the second construct because it's way more readable, and probably performs better.

The compiler can easily optimize temporary variables away, but cannot determine if a function call has sideeffects. So the double function call cannot be optimized to one single call.


The second one can't be slower, so I suppose I'd say the second one, as it avoids a method call at least. But, the JIT is going to inline this. The performance difference is going to be all but negligible unless this is in a very tight loop.

I personally find the second more readable as well (perhaps if you called the variable something other than 'temp'). So I'd vote for it on those grounds. But that's the better question here I think: which is clearer? Worry about performance when you have any reason to believe this is code matters.


Second version better, though it has a tiny performance than first, it also has couple of advantages 1. Readability 2. Maintenance - For this type of code, suppose if you have to check for more conditions(say 10), then to change the logic, just one place.

0

精彩评论

暂无评论...
验证码 换一张
取 消