1 , JAVA Performance Tuning-do not use expressions in loop conditions
When we learn about Java Performance Tuning, we often see this passage: In the case of No compilation optimization, in the loop, the loop condition will be repeatedly calculated, if you do not use complex expressions, and the loop condition value is not changed, the program will run faster.
Import Java.util.vector;
Class Cel {
void method (vector vector) {
for (int i= 0; i < vector.size (); i++)//violation
; //...
}
}
Correct:
Class Cel_fixed {
void method (vector vector) {
int size =vector.size ()
for (int i= 0; i < size; i++)
; //...
}
}
Logically, it seems very reasonable, but the efficiency of the increase of about how much, in code to test under:
2 , whether there is an expression comparison test source code in the loop condition
Package com.java.test;
Import java.util.ArrayList;
Import java.util.List;
/**
* using warm -up mode
* JVM parameter:-xx:printcompilation
* Objective: To test the effect of cyclic conditions on performance in a loop.
* @author Fan Fangming
*/
public class Easyloopparamtest {
List<string>alist = new arraylist<string> ();
Public staticvoid Main (string[] args) throws exception{
intwarmupcycles = 100000;// warm -up times
inttestcycles = 200000;// number of official executions
Easyloopparamtestse = new Easyloopparamtest ();
System.out.println (" ... the preheating cycle begins ... ");
Longcomputetime = Se.runcomputeparam (warmupcycles);
Long fixtime= Se.runfixparam (warmupcycles);
System.out.println (" ... preheating End ");
System.out.println (" ... during the preheating phase, the average time for dynamic calculation of cyclic adjustments is: "+ computetime);
System.out.println (" ... during the preheating phase, the average time for fixed parameters is: "+ fixtime);
System.out.println (" ... into the formal cycle ... ");
Computetime= Se.runcomputeparam (testcycles);
Fixtime =se.runfixparam (testcycles);
System.out.println (" ... during the formal operation phase, the average time for dynamic calculation of cyclic adjustments is: "+ computetime);
System.out.println (" ... during the formal operation phase, the average time for fixed parameters is: "+ fixtime);
}
// add 1 elements to a queue
Publiceasyloopparamtest () {
for (inti=0; i< 10000; i + +) {
Alist.add (string.valueof (i));
}
}
// one in the loop, the loop condition is calculated repeatedly
Private Longruncomputeparam (int iterations) {
// Start Timing
Longstarttime = System.nanotime ();
for (int loop= 0; loop < iterations; Loop + +) {
for (inti = 0; i < alist.size (); i++) {
// Simplifying Internal Logic
}
}
// Timing Complete
Longelapsedtime = System.nanotime ();
Return (elapsedtime-starttime)/iterations;
}
// The other one in the loop, the loop condition is the same.
Public Longrunfixparam (int iterations) {
// Start Timing
Longstarttime = System.nanotime ();
int size =alist.size ();
for (int loop= 0; loop < iterations; Loop + +) {
for (inti = 0; i < size; i++) {
// Simplifying Internal Logic
}
}
// Timing Complete
Longelapsedtime = System.nanotime ();
Return (elapsedtime-starttime)/iterations;
}
}
3 , running results
... Warm-up cycle begins ...
... Preheating End
... During the preheating phase, the average time to dynamically calculate cycle adjustments is: 60456
... During the preheating phase, the average time to fix parameters is: 3537
... Into the formal cycle ...
... During the formal operation phase, the average time to dynamically calculate cycle adjustments is: 41578
... During the formal operation phase, the average time for fixed parameters is: 3493
4 , conclusions
In the loop, the loop condition is calculated repeatedly, and if you do not use a complex expression, the increase in efficiency is 11 times times greater.
If there are a lot of similar loops in the application, with this small adjustment, the overall performance of the system will be improved considerably.
Java Performance Tuning-do not use an expression in a loop condition