An algorithm should know how to measure the time efficiency and space efficiency. However, more research measures the time efficiency, and the rapid development of hardware is not so important. How to measure the time efficiency is the number of basic operations. The number of basic operations. For example, to calculate the maximum value in a one-dimensional array:
[Java]
Public class ArrayTester2 {
/**
* @ Param args
*/
Public static void main (String [] args ){
Int [] array = {10, 20, 30, 40, 50, 60 };
Int max = array [0];
For (int I = 1; I <array. length; I ++ ){
If (array [I]> max ){
Max = array [I];
}
}
System. out. printf (max );
}
}
Public class ArrayTester2 {
/**
* @ Param args
*/
Public static void main (String [] args ){
Int [] array = {10, 20, 30, 40, 50, 60 };
Int max = array [0];
For (int I = 1; I <array. length; I ++ ){
If (array [I]> max ){
Max = array [I];
}
}
System. out. printf (max );
}
}
In the above example, the number of basic operations is the comparison of the if statement: array [I]> max, because this operation is executed in each loop, while max = array [I], it is executed only when the if statement is set up. It can be summarized as a rule, that is, the number of basic operations is the most time-consuming operation in the algorithm's most memory loop.