In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the small details of improving the running efficiency of Java code". In the daily operation, I believe many people have doubts about the small details of improving the running efficiency of Java code. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the small details of improving the running efficiency of Java code?" Next, please follow the editor to study!
Code optimization details
1. Try to specify final modifiers for classes and methods
Classes with final modifiers are not derivable. In the core API of Java, there are many examples of using final, such as java.lang.String, where the whole class is final. Assigning a final modifier to a class makes the class uninheritable, and assigning a final modifier to a method makes it impossible for a method to be overridden. If you specify a class as final, all the methods of that class are final. The Java compiler will look for opportunities to inline all final methods, which play an important role in improving the efficiency of Java. For more information, see Java runtime optimization. This can improve performance by an average of 50%.
two。 Reuse objects as much as possible
In particular, when using String objects, you should use StringBuilder/StringBuffer instead of string concatenation. Because the Java virtual machine not only takes time to generate objects, it may also take time to garbage collect and process these objects in the future, so generating too many objects will have a great impact on the performance of the program.
3. Use local variables whenever possible
The parameters passed when the method is called and the temporary variables created in the call are saved in the stack faster, while other variables, such as static variables, instance variables, and so on, are created in the heap and are slow. In addition, the variables created in the stack are gone as the method ends, and no additional garbage collection is required.
4. Close the stream in time
In the process of Java programming, you must be careful when you connect to the database and operate the I _ big O stream, and close it in time to release resources after using it. Because the operation of these large objects will cause great overhead of the system, a little carelessness will lead to serious consequences.
5. Minimize the double calculation of variables
Make clear a concept, the call to the method, even if there is only one sentence in the method, there is consumption, including creating stack frames, protecting the scene when the method is called, restoring the site when the method is called, and so on. So for example, do the following:
For (int I = 0; I
< list.size(); i++) {...} 建议替换为: for (int i = 0, int length = list.size(); i < length; i++) {...} 这样,在list.size()很大的时候,就减少了很多的消耗 6. 尽量采用懒加载的策略,即在需要的时候才创建 例如: String str = "aaa";if (i == 1) { list.add(str); } 建议替换为: if (i == 1) { String str = "aaa"; list.add(str); } 7. 慎用异常异 常对性能不利。抛出异常首先要创建一个新的对象,Throwable接口的构造函数调用名为fillInStackTrace()的本地同步方法,fillInStackTrace()方法检查堆栈,收集调用跟踪信息。只要有异常被抛出,Java虚拟机就必须调整调用堆栈,因为在处理过程中创建了一个新的对象。异常只能用于错误处理,不应该用来控制程序流程。 8. 不要在循环中使用try…catch…,应该把其放在最外层 除非不得已。如果毫无理由地这么写了,只要你的领导资深一点、有强迫症一点,八成就要骂你为什么写出这种垃圾代码来了。 9. 如果能估计到待添加的内容长度,为底层以数组方式实现的集合、工具类指定初始长度 比如ArrayList、LinkedLlist、StringBuilder、StringBuffer、HashMap、HashSet等等,以StringBuilder为例: StringBuilder() // 默认分配16个字符的空间 StringBuilder(int size) // 默认分配size个字符的空间 StringBuilder(String str) // 默认分配16个字符+str.length()个字符空间 可以通过类(这里指的不仅仅是上面的StringBuilder)的来设定它的初始化容量,这样可以明显地提升性能。比如StringBuilder吧,length表示当前的StringBuilder能保持的字符数量。因为当StringBuilder达到最大容量的时候,它会将自身容量增加到当前的2倍再加2,无论何时只要StringBuilder达到它的最大容量,它就不得不创建一个新的字符数组然后将旧的字符数组内容拷贝到新字符数组中—-这是十分耗费性能的一个操作。试想,如果能预估到字符数组中大概要存放5000个字符而不指定长度,最接近5000的2次幂是4096,每次扩容加的2不管,那么: 在4096 的基础上,再申请8194个大小的字符数组,加起来相当于一次申请了12290个大小的字符数组,如果一开始能指定5000个大小的字符数组,就节省了一倍以上的空间; 把原来的4096个字符拷贝到新的的字符数组中去。 这样,既浪费内存空间又降低代码运行效率。所以,给底层以数组实现的集合、工具类设置一个合理的初始化容量是错不了的,这会带来立竿见影的效果。但是,注意,像HashMap这种是以数组+链表实现的集合,别把初始大小和你估计的大小设置得一样,因为一个table上只连接一个对象的可能性几乎为0。初始大小建议设置为2的N次幂,如果能估计到有2000个元素,设置成new HashMap(128)、new HashMap(256)都可以。 10. 当复制大量数据时,使用System.arraycopy()命令 11. 乘法和除法使用移位操作 例如: for (val = 0; val < 100000; val += 5) { a = val * 8; b = val / 2; } 用移位操作可以极大地提高性能,因为在计算机底层,对位的操作是最方便、最快的,因此建议修改为: for (val = 0; val < 100000; val += 5) { a = val >1;}
The shift operation is fast, but it may make the code difficult to understand, so it's best to comment accordingly.
twelve。 Don't keep creating object references within the loop
For example:
For (int I = 1; 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.