Some small points in Java efficiency programming

Source: Internet
Author: User

1. In the event of a string connection in the program, use StringBuffer instead of string, which reduces the memory consumption caused by multiple string creation and garbage collection

2. Use local variables as much as possible. The arguments passed when the method is called and the temporary variables created in the call are saved in the stack because the JVM is a stack-based virtual machine, so the processing of the stack is optimized, and the static variables and instance variables of the class are created on the heap. Therefore, the execution efficiency of using local variables is higher than that of using static variables and instance variables of classes.

3.for (int i = 0; I<list.size (); i++) {

}

Try to use the following method instead of the way above

int count = List.size ();

for (int i = 0; i<count; i++) {

}

Pardon my shameful Mark's behavior: the original address http://blog.csdn.net/zhaokaiqiang1992/article/details/26157109

This can reduce the number of operations, such as our program below

Package Test;import Java.util.arraylist;import Java.util.list;public class Testforfast {public static void main (string[ ] args) {list<string> List = new arraylist<string> ();//Create a collection containing large data for (long i = 0; i < 1000000; i++) {list.a DD ("Xixixi" +i);} Long starttime1 = System.currenttimemillis (); for (int i=0;i<list.size (); i++) {//Perform some operations}long endtime1 = System.currenttimemillis (); System.out.println ("The first Wasted Time is:" + (ENDTIME1-STARTTIME1)); Long starttime2 = System.currenttimemillis (); int count = List.size (); for (long i=0;i<count;i++) {//Perform some operations}long endtime2 = System.currenttimemillis (); System.out.println ("The second Wasted Time is:" + (Endtime2-starttime2));}}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Some small points in Java efficiency programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.