JAVA program performance optimization - easy to make mistakes

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

change to
int size=list.size();
for(int i=0;i<size;i++){
}

 1. The code that is repeatedly called in the separation loop is as above. The bold part will be called every iteration, and the same value will be returned every time. Separating all similar codes has a positive meaning to improve the performance of the loop, especially the larger the number. more, resulting in better results

String a="";
for(int i;i<100;i++){
    a+="hello word";
}

This modification becomes
StringBuffer would be better, add buffering

 2. When encountering character splicing and file IO stream, try to use Buffer (buffer) to relieve the pressure of reading and writing and reduce the performance bottleneck caused by IO



 

3. When encountering frequently used calculation results or data, you can consider introducing a cache to reduce the pressure on the database, thereby improving the user's access speed.

 

for(int i=0;i<100000000;i++){
try{
System.out.print("ddddd");
}catch(){
}
}
The above code takes about 110ms
Modified to the following
try{
 for(int i=0;i<100000000;i++){

 System.out.print("ddddd");

 }
}catch(){
}
 It takes about 62ms

 4. Use exceptions with caution

 5.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326390900&siteId=291194637