简体   繁体   中英

Performance difference between util method and writing same code in the method…?

private void test(){
   List<String> list=new ArrayList<>();
   list.add("aaa");
   list.add("bbb");
   list.add("ccc");

   //using utility
   boolean flag = Util.search(list,"bbb");
   flag = Util.search(list,"aaa");

   //using same code here only
   for(String obj:list){
      if(obj.equals("bbb")){
         return true;
      }
   }

   for(String obj:list){
      if(obj.equals("aaa")){
         return true;
      }
   }
   // util method has same logic as in the method for search
}

My question is which is better: a utility method or same logic in test method (regarding performance and for example)?

Forget about performance, that is negligible difference, write the smallest methods possible so that the code is easy to maintain. Extract the code out of your method and use the util method.

And one more thing is your method can be reduced as

if(list.contains("aaa")){
return true;
}

No need of that for loop. Make use of contains method in list

There shouldn't be any noticeable performance difference.

You should do what you believe is clearer and easier to maintain.

The reason there is little difference is that the JIT can inline methods and you end up with the same code at runtime once it is optimised.

Where performance does matter is when you have large collections and different time complexities. Eg say you want to do

if (words.contains("bbb"))
    return true;

If you use a List this is an O(N) operation however if you use a HashSet this should be an O(1) operation. This can make far more difference than almost anything else you can do.

In short, stick to O(1) or O(log N) or very small collections, and you should be fine, unless you profiler indicates you have a problem in a specific place, I wouldn't second guess how the code will perform.

Note: There is a situation where a utility method can out perform inlined code. For code to be optimised, it has to be run a certain number of times or loop a certain number of times. (The exact number of loops, calls depends on the VM and which stage of compilation)

A method which is called a lot (or loops a lot) as it is called from many places will be optimised sooner than the same code repeated in many places.

One of the most important rules in programming: do not duplicate code . Never. Ever.

Programming is like building new roads: people keep forgetting that each road that was build in the past will require maintenance in the future.

Every time you copy code you create another spot that might need to be touched when you want to do something different. Besides that, many studies have found, sooner or later, your "duplicated code" pieces will start to be slightly different (because you change one spot, and you forget about another one; or you need a little change here; but only here; and not there). And typically, when you have a lot of duplicated code with subtle differences ... that is where you will have bugs.

Long story short: understand the language, and how the JIT works before even thinking about performance tuning. In the mean time: focus on writing code that is easy to read and maintain. Anything else is a huge waste of time.

My question is which is better utility method or same logic?

Difference in performance should not be really significant, if your custom method implements the same logic as the utility one.

My advice is: Do not re-invent the wheel!

When you have something that is thoroughly tested, verified and maybe optimized, just re-use it. Otherwise, if you try to re-implement everything on your own, the chances to make a mistake and other issue is big.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM