In the design of php to generate static pages, there are more and more static pages. In the process of continuously generating html, the efficiency is getting slower and slower, so it is imperative to optimize php statements, first, check the php html generated by myself. It turns out to be a direct loop without any optimizations. Let's take a look at the PHP loop statement before the summer blog:
$ Stat = time (); for ($ I = 1; $ I <= 10000; $ I ++) {$ strTemp = $ strTemp. "<option value = '". $ I. "'"; $ strTemp = $ strTemp. "> Number ". $ I. "page </option>" ;}$ end = time (); echo $ end-$ stat;
This loop simplifies the generation of static php statements. It mainly describes the efficiency time of loop generation. The specific html generation is not described here. For more information, see the previous summer blog article, here we use php to execute a loop for 10000 times. If it becomes 10000 00, the loop speed will get slower and the number of seconds for echo will get larger and larger, the final solution is to make a simple transformation of this PHP loop statement, as follows:
$ Stat = time (); $ strTemp = ''; for ($ I = 1; $ I <= 10000; $ I ++) {$ strTemp. = "<option value = '". $ I. "'"; $ strTemp. = "> No ". $ I. "page </option>" ;}$ end = time (); echo $ end-$ stat;
It does not take one second to run again, and the time efficiency is very fast. We can see from the comparison of the two sections of code that the first section is to re-assign values after each loop is executed, if you want to assign a value for 10000 times, the speed will be slow. The second code uses the dot symbol to directly connect the value to a string and assign a value only once. Therefore, the efficiency is very high, we can put the following code in any loop statement that executes multiple times, such as when php generates multiple html lines.
The implementation code is as follows:
<? Php $ Stime = 0; $ Etime = 0; $ Ttime = 0; $ Stime = microtime (true); // Obtain the execution start time of the program // echo $ Stime. "<br/>"; for ($ I = 1; $ I <= 10000000; $ I ++) {}// to achieve a certain time difference, therefore, a FOR statement is used to consume some resources. $ Etime = microtime (true); // get the execution end time of the program // echo $ Etime. "<br/>"; $ Ttime = $ Etime-$ Stime; // calculate the difference value // echo $ Ttime. "<br/>"; $ str_total = var_export ($ Ttime, TRUE); if (substr_count ($ str_total, "E ")) {// in order to avoid the appearance of results such as 1.28746032715E-005, we have done some processing. $ float_total = floatval (substr ($ str_to Tal, 5); $ Ttime = $ float_total/100000;} echo $ Ttime. 'Second';?>
The development engineer of baiheng introduced the related functions:
The microtime () function returns the current Unix timestamp and the number of microseconds.
The var_export () function returns the structure information about the variables passed to the function. It is similar to var_dump (). The difference is that the returned representation is a valid PHP code.
The substr_count () function calculates the number of times a substring appears in a string.
Note: engineers from Nanchang website production company reminded developers: $ Stime = microtime (true); to be placed at the top of the page, $ Etime = microtime (true ); the location of the page to be placed. Otherwise, the time cannot be calculated.