With the continuous upgrading of hardware configuration, for small and medium sized applications, the space complexity of the algorithm requirements are also relaxed a lot. However, in today's Web2.0 era, the time complexity of the application has a higher demand.
What is the time complexity of the algorithm? In summary, it means to choose an algorithm to represent the original operation of the algorithm, the number of repeated execution of the original operation as the time measurement of the algorithm. There are two factors affecting time complexity: one is the execution time of the original operation and the other is the execution times caused by the control structure of the original operation. In order to reduce the time complexity of the algorithm, it is an easy way and the main method to decrease the execution times of the original operation. The method described in this paper is to use the PHP array, to reduce the number of execution times, so as to reduce the complexity of the algorithm time requirements, and share with you.
The time measurement of the algorithm is recorded as T (n) =o (f (n)), it indicates that the number of times the basic operation in the algorithm is repeated is a function f (n) of the problem scale n, which means that the growth rate of the algorithm execution time and the growth rate of f (n) are the same as the increase of the problem size n. In most cases, we discuss the time complexity of the algorithm as the original operation in the deepest loop, because it executes the same number of times as the statement that contains it. In general, the time complexity of the algorithm can only be discussed by selecting a basic operation for a problem. Sometimes you also need to consider a variety of basic operations.
In WEB development, the execution time or response time of a function is usually not only related to the responsiveness and processing power of the server, but also to the interaction time of the Third-party tools, such as the time to link to the database and the time to access the data. Therefore, in the selection of the original operation is, the need to comprehensively consider the application of various factors, the maximum impact program execution time operation for the original operation, to measure the time complexity of the algorithm. In other words, programmers need to have a basic understanding of the execution time of important operations when they write code.
Let's take a look at an example, assuming that the development language of the WEB program is PHP, the background of the DB2 database, PHP through PEAR::D b Data Abstraction layer to achieve access to the database.
The database contains student table STUDENTS (see table 1), Class table CLASSES (see table 2), Student score table SCORES (see table 3), the name and class of the students who have scored more than 90 on the math score in this exam will be displayed on the Web page.
Table 1. STUDENTS Table
Column Name |
Describe |
Sid |
School Number |
Stuname |
Name |
GENDER |
Gender |
Age |
Age |
CLASSID |
Class number |
... |
|
Table 2. CLASSES Table
Column Name |
Describe |
CLASSID |
Class number |
CLASSNAME |
Class name |
... |
|
Table 3. SCORES Table
Column Name |
Describe |
Sid |
Student study Number |
COURSE |
Subject |
SCORE |
Results |
... |
|
Depending on your personal programming habits, there are usually two ways to solve this problem (the operation of accessing the database is expressed in PEAR::D b), see Methods 1, 2.
[Method 1] to STUDENTS, CLASSES, SCORES three tables to do a joint query, one time to obtain the conditions of student information and class information. The PHP algorithm is described as follows:
$QUERYSTR = "SELECT distinct S.stuname as stuname,c.classname as CLASSNAME".
From STUDENTS as s,classes as c,scores as R ".
" where S.sid=r.sid and S.classid=c.classid and r.course= ' Math '.
and r.score>=90 ";
$result = $db 2handle->query ($QUERYSTR); Get data from the database
while ($row = $result->fetchrow (DB_FETCHMODE_ASSOC)) {
//Read and display the data
echo "Studentname=". $ row[' Stuname ']. " \ classname= ". $row [' ClassName ']." \ n ";
} Done
|
[Method 2] find the student number that satisfies the condition from the SCORES table, then look for the student's name and class code from the STUDENTS table, and finally get the class name in the CLASSES table. The PHP algorithm is described as follows:
$SCORESTR = "SELECT distinct SID from SCORES where course= ' Math ' and score>=90";
$scoredata = $db 2handle->query ($SCORESTR);
To obtain the student's school number from the database while ($score = $scoredata->fetchrow (DB_FETCHMODE_ASSOC)) {//Read the student's number and find the student's name and class number in the students table
$studentstr = "Select Stuname,classid from STUDENTS where sid= '". $score [' SID ']. "'";
$studata = $db 2handle->query ($STUDENTSTR);
$stu = $studata->fetchrow (DB_FETCHMODE_ASSOC); Displays the student's name echo "Studentname=". $stu [' Stuname ']. "
\ t ";
Read the student's class number and look in the CLASSES table for the student's class name $classstr = "Select CLASSNAME from CLASSES where classid= '". $stu [' CLASSID ']. "'";
$classdata = $db 2handle->query ($CLASSSTR);
$class = $classdata->fetchrow (DB_FETCHMODE_ASSOC); Show the student's class echo "Classname=". $class [' CLASSNAME ']. "
\ n "; }//end while to getting each student ' s ID. Done |
For such an algorithm description, I believe that we will have déjà vu feeling. This is also a widely used algorithm for most programmers. Because has been accustomed to the thinking of the algorithm logic directly into the code, and often do not have the time and mind to consider the pros and cons of the algorithm. Here to analyze the time complexity of the two algorithms.
Because the WEB server reads and displays data for a relatively small amount of time, typically in the order of magnitude 10ms, the order of magnitude of the time to query and get data from the DB2 database will be the order of magnitude 100ms, and increase with the amount of query data. Therefore, the query database operation can be used as a measure of time complexity of the original operation, to STUDENTS table and SCORES table data volume as the problem size n (usually, CLASSES table data volume is small and relatively stable).
For Method 1, the number of accesses to the database is constant 1 as the problem scale n increases. Thus, the complexity of Time is T (n) =o (1). For Method 2, assuming that the record that satisfies the condition in the SCORES table has m, the original operation is performed m+1. That is to say, with the increase of the data scale n, the execution times of the original operation increase linearly. The visible time complexity is T (n) =o (n). Thus, the time complexity of Method 1 is low.
So where is the problem with approach 1? Mainly because Method 1 will increase the database load, that is, the execution time of the original operation is affected by the problem scale N. Suppose the number of records in the Students,classes,scores is X, Y, Z, respectively. Then, when performing a federated query operation, a matrix of x*y*z is formed in the database, then the number of records that satisfies the condition is found in the matrix, and the Stuname information and CLASSNAME of the records are finally obtained. In this way, the increase in data in any one table results in the multiplication of records in the matrix table.
Main ideas: In the case where the data is relatively simple and stable, the subscript (Index) of the PHP Array (array) can be used as the character of string, and the data is temporarily stored in the array. In this way, the required values can be obtained quickly by subscript (Index), thus reducing the number of queries to the database, and thus reducing the time complexity of the algorithm.
[Method 3] to obtain CLASSID and CLASSNAME from the CLASSES table in the Classarray one-dimensional array, from the STUDENTS table to obtain the SID and Stuname and CLASSID of the corresponding Relationships are stored in a two-dimensional array of stuarray. Then find the student number that satisfies the condition from the SCORES table, read the student's name and class number from the Stuarray array, and read the class name from the Classarray. The PHP algorithm is described as follows:
$ClassArray = Array ();
$StuArray = Array ();
$classstr = "Select Classid,classname from CLASSES";
$classdata = $db 2handle->query ($CLASSSTR); while ($class = $classdata->fetchrow (DB_FETCHMODE_ASSOC)) {//Generate Classarray Array, subscript index is named ClassID, and the corresponding value is classname $
classarray[$class [' CLASSID ']] = $class [' CLASSNAME '];
}//end while $ClassArray $stustr = "Select Sid,stuname,classid from STUDENTS";
$studata = $db 2handle->query ($STUSTR); while ($stu = $studata->fetchrow (DB_FETCHMODE_ASSOC)) {//Generate Stuarray Array, subscript index is named by SID, corresponding value is Stuname and ClassID $
stuarray[$stu [' SID ']][' stuname '] = $stu [' Stuname '];
$StuArray [$stu [' SID ']][' CLASSID '] = $stu [' CLASSID '];
}//end while $StuArray $scorestr = "SELECT distinct SID from SCORES where course= ' Math ' and score>=90";
$scoredata = $db 2handle->query ($SCORESTR); To obtain the student's number from the database while ($score = $scoredata->fetchrow (DB_FETCHMODE_ASSOC)) {//Read the student's number and read the student's name from the Stuarray. Reads the class name echo "Studentname=" from the Classarray. $StuArray [$score [' SID '] [' stuname ']."T"; echo "Classname=". $ClassArray [$StuArray [$score [' SID ']] [' CLASSID ']]. "
\ n "; }//end while to getting each student ' s ID.
Done |
The time complexity of the improved method is still T (n) =o (1). Compared to Method 1, Method 3 does not have to worry about multiplying the cost of database queries as a result of the increase in records in one table. Compared with Method 2, the time complexity decreases and the algorithm space complexity is not affected. Kill both birds.
Although this optimization method is simple and easy to use, it does not mean that it is omnipotent. You need to consider the "degree" problem when you use it. Assuming that the data volume of STUDENTS table is very large, then the consumption of the system memory is increased when generating stuarray, so the spatial complexity of the algorithm will be affected. In addition, when the data volume is large enough, the main factors that affect the execution time of the algorithm have changed, and the original operation needs to be selected again. In view of the STUDENTS table record number, the CLASSES table record less and stable situation, we can consider the combination of nested query and array to optimize the algorithm. Method 4 is given here for reference.
[Method 4] obtains CLASSID and CLASSNAME correspondence from the CLASSES table into the Classarray one-dimensional array. From the SCORES table, the student number of the students who meet the conditions is queried, and the STUDENTS table is queried to obtain the students ' stuname and CLASSID. The name of the class is then read from the Classarray. The PHP algorithm is described as follows:
$ClassArray = Array ();
$classstr = "Select Classid,classname from CLASSES";
$classdata = $db 2handle->query ($CLASSSTR); while ($class = $classdata->fetchrow (DB_FETCHMODE_ASSOC)) {//Generate Classarray Array, subscript index is named ClassID, and the corresponding value is classname $
classarray[$class [' CLASSID ']] = $class [' CLASSNAME '];
}//end while $ClassArray $stustr = "Select Stuname,classid from STUDENTS where SID in".
"(select distinct SID from SCORES where course= ' M ' and score>=90)";
$studata = $db 2handle->query ($STUSTR); Gets the student's name and class number from the database while ($stu = $studata->fetchrow (DB_FETCHMODE_ASSOC)) {//reads the student's name and reads the class name from the Classarray Echo "Studentname=". $stu [' Stuname ']. "
\ t "; echo "Classname=". $ClassArray [$stu [' CLASSID ']]. "
\ n "; }//end while to getting each student ' s Info. Done |
Method 3 and Method 4 refer to the small technique of array, which subtly reduces the time complexity of the algorithm. In the practical application, the algorithm logic is much more complicated, and the optimization of the algorithm needs to consider many factors. What you need to propose is that the methods described in this article apply not only to PHP applications. If the programming language array supports the use of strings as subscript, we can consider using the method proposed in this paper: skillfully using the subscript of the array to reduce the time complexity of the algorithm. For programming languages that do not support string arrays, consider creating a hash table to achieve the same effect.