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