In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the skills to improve code performance". In daily operation, I believe that many people have doubts about the skills of improving code performance. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the skills to improve code performance?" Next, please follow the editor to study!
1. Make the code perform better 1.1. When you need the primary key and values of Map, you should iterate over entrySet ()
Iterating over keySet () is correct when only the primary key of Map is needed in the loop. However, iterating over entrySet () is more efficient when primary keys and values are needed, and it is better to iterate over keySet () and then get values.
Counterexample: Map map =...; for (String key: map.keySet ()) {String value = map.get (key);.} positive example: Map map =...; for (Map.Entry entry: map.entrySet ()) {String key = entry.getKey (); String value = entry.getValue ();.} 1.2. Should use Collection.isEmpty () to detect null
Using Collection.size () to detect null is logically fine, but using Collection.isEmpty () makes the code easier to read and better performance. The time complexity of any Collection.isEmpty () implementation is O (1), but some Collection.size () implementations may be O (n).
Counterexample: if (collection.size () = = 0) {...} positive example: if (collection.isEmpty ()) {.}
If you also need to detect null, you can use CollectionUtils.isEmpty (collection) and
CollectionUtils.isNotEmpty (collection).
1.3. Don't pass the collection object to yourself.
The method of passing the collection as a parameter to the collection itself is either an error or meaningless code.
In addition, because some methods require parameters to remain unchanged during execution, passing the collection to itself may result in abnormal behavior.
Counterexamples: List list = new ArrayList (); list.add ("Hello"); list.add ("World"); if (list.containsAll (list)) {/ / meaningless, always returning true...} list.removeAll (list); / / poor performance, directly using clear () 1.4. Set initialization to specify the size as much as possible
The collection class of java is very convenient to use, but as you can see from the source code, the collection is also limited in size. The time complexity of each expansion is likely to be O (n), so specifying a predictable set size as far as possible can reduce the number of set expansion.
Counterexample: int [] arr = new int [] {1,2,3}; List list = new ArrayList (); for (int I: arr) {list.add (I);} positive example: int [] arr = new int [] {1,2,3}; List list = new ArrayList (arr.length); for (int I: arr) {list.add (I);} 1.5. String concatenation using StringBuilder
General string concatenation is optimized by java at compile time, but string concatenation in a loop cannot be optimized at java compilation time, so StringBuilder is needed to replace it.
Counterexample: String s = ""; for (int I = 0; I < 10; iTunes +) {s + = I;} positive example: String a = "a"; String b = "b"; String c = "c"; String s = a + b + c; / / No problem, the java compiler will optimize StringBuilder sb = new StringBuilder (); for (int I = 0; I < 10; iTunes +) {sb.append (I) / / in the loop, the java compiler cannot optimize, so use the random access of StringBuilder} 1.6.List manually
Everyone knows the difference between arrays and linked lists: random access to arrays is more efficient. After calling the method to get the List, what if you want to randomly access the data in it and don't know whether the internal implementation of the array is a linked list or an array? You can determine whether it implements the RandomAccess interface.
Positive example: / / call someone else's service to get list List list = otherService.getList (); if (list instanceof RandomAccess) {/ / internal array implementation, you can randomly access System.out.println (list.get (list.size ()-1));} else {/ / internal may be linked list implementation, random access efficiency} 1.7. To call the Collection.contains method frequently, use Set
In the java collection class library, the general time complexity of List's contains method is O (n). If you need to call the contains method to find data frequently in the code, you can first convert list into HashSet implementation, reducing the time complexity of O (n) to O (1).
Counterexample: ArrayList list = otherService.getList (); for (int I = 0; I
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.