The test data is real, but only for reference. Welcome to the discussion.
In addition to comparison, it can also be used by PHP and JSP programmers to optimize their own programs.
I. Arithmetic Operations A. Test Standards
Loop n times and perform integer auto-increment operation.
B. Test
Test code:
Test Type |
JSP int integer |
JSP long integer |
JSP long integer 2 |
PHP |
PhP2 |
Test code |
Int I = 0; While (I <XXXX) { I ++; } |
Long L = 0; While (L <XXXX) { L ++; } |
Long L = 0; While (L <XXXX) { L = L + 1; } |
$ I = 0; While ($ I <XXXX) { $ I ++; } |
$ I = 0; While ($ I <XXXX) { $ I = $ I + 1; } |
Test result (unit: milliseconds)
Test result Description: M-N indicates that the main fluctuation range is between M and N. N indicates that there are many values or average values, and M (x, y) indicates that X appears occasionally, y. The following are the same.
XXXX Value |
JSP int integer |
Jsplong integer |
Jsplong integer 2 |
PHP |
PhP2 |
1000 |
0 |
0 |
0 |
0-1 |
0-1 |
10000 |
0 |
0 |
0 |
3-4 |
6-7 |
100000 |
0 |
0 |
0 (16, 32) |
34-35 |
51-52 |
1000000 |
0 (10) |
0 (16, 32) |
0 (16, 32) |
348-368 |
527-534 |
10000000 |
0 (13) |
16-32 |
32-78 |
3547-3585 |
5239-5390 |
100000000 |
0 (16) |
266-281 |
265-281 |
35309-35486 |
1000000000 |
0 (16,743) |
2625-2676 |
2203-3687 |
C. Conclusion
Java's arithmetic operations are well optimized. It is estimated that they directly correspond to CPU commands, and the numeric values are large, so the arithmetic operation performance is stable.
It should be noted that the long operation time is longer than the int operation time (after all, it is a 32-bit system). Therefore, do not use long when long is not needed, int.
PHP is a weak type of variable, and the arithmetic operation is not satisfactory. It is estimated that it is not an arithmetic operation that directly corresponds to the machine code.
The PHP arithmetic operation process is estimated as follows. before the operation, you need to check whether the variable is a numerical value and convert the non-numeric value to a numerical value. For example, PHP can perform this operation: 100 + "zhoutang", and the result is 100. it is precisely because it is a weak type language, coupled with detection and conversion before the operation that leads to low performance of arithmetic operations.
PHP's time for a single operation is relatively stable, and the time spent on every 10000 arithmetic addition operations is about 3.5 ms.
Different calculation methods also affect the performance.
Ii. String operations A. Test Standards
String connection operation comparison.
B. Test
Test code:
Test Type |
JSP |
PHP |
Test code |
String STR = ""; While (Str. Length () <XXXX) { STR + = ""; } |
$ STR = ""; While (strlen ($ Str) <XXXX) { $ Str. = ""; } |
Test result (unit: milliseconds)
XXXX Value |
JSP |
PHP |
1000 |
0-16 |
1 |
10000 |
656-703 |
9-10 |
100000 |
105078-105235 |
95-103 |
C. Conclusion
Java strings are processed by objects. Obviously, Java is far from PhP during string comparison. (Because of string operations, most of them need to be connected. Here we only compare the join operations, and roughly compare the substring. The difference between the two is not great)
Many PHP string operations are probably directly called C's string functions, so the efficiency is much higher.
In web development, string connection operations are still frequent (including SQL string generation in many cases). Therefore, in terms of arithmetic operations and string operations, Java (JSP) the advantage is not obvious, and it has its own strengths with PHP.
Iii. database query a. Test Standards
The database is opened, queried, and closed N times in a loop. It is mainly for JSP direct connection to the database, JSP connection pool connection to data training, PHP direct connection to the database, PHP persistent connection to the database for comparison.
B. Test Results
Test code:
Test Type |
Test code |
JSP |
String dburl, dbuser, dbpwd, strsql; Dburl = "JDBC: mysql: // localhost: 3306/zhoutang "; Dbuser = "root "; Dbpwd = ""; Strsql = "Update tbluser set userlc = userlc + 1 where userid = 100 ";Class. forname ("com. MySQL. JDBC. Driver "); Connection con; Statement stmt; Int I = 0; While (I <XXX) { Con = drivermanager. getconnection (dburl, dbuser, dbpwd ); Stmt = con. createstatement (); Stmt.exe cuteupdate (strsql ); Stmt. Close (); Con. Close (); Stmt = NULL; Con = NULL; I ++; } |
JSP connection pool |
Context CTX = new initialcontext (); Datasource DS = (datasource) CTX. Lookup ("Java: COMP/ENV/jdbc/MySQL ");String strsql = "Update tbluser set userlc = userlc + 1 where userid = 100 "; Connection con; Statement stmt; Int I = 0; While (I <XXXX) { Con = Ds. getconnection (); Stmt = con. createstatement (); Stmt.exe cuteupdate (strsql ); Stmt. Close (); Con. Close (); Stmt = NULL; Con = NULL; I ++; } |
PHP |
$ Dbhost = "localhost "; $ Dbuser = "root "; $ Dbpwd = ""; $ Dbname = "zhoutang "; $ Strsql = "Update tbluser set userlc = userlc + 1 where userid = 100 ";$ I = 0; While ($ I <XXXX) { $ Link = mysql_connect ($ dbhost, $ dbuser, $ dbpwd) Or die ('could not connect: '. mysql_error ()); Mysql_select_db ($ dbname ); Mysql_query ($ strsql ); Mysql_close ($ link ); $ I ++; } |
PHP persistent connection |
$ Dbhost = "localhost "; $ Dbuser = "root "; $ Dbpwd = ""; $ Dbname = "zhoutang "; $ Strsql = "Update tbluser set userlc = userlc + 1 where userid = 100 ";$ I = 0; While ($ I <XXXX) { $ Link = mysql_pconnect ($ dbhost, $ dbuser, $ dbpwd) Or die ('could not connect: '. mysql_error ()); Mysql_select_db ($ dbname ); Mysql_query ($ strsql ); Mysql_close ($ link ); $ I ++; } |
Test result (unit: milliseconds)
XXXX Value |
JSP |
JSP connection pool |
PHP |
PHP persistent connection |
50 |
437-600 |
31-50 |
219-232 |
34-37 |
100 |
890-1047 |
62-94 |
448-468 |
69-73 |
200 |
2141-2263 |
157-189 |
948-979 |
159-175 |
500 |
5078-5140 |
375 |
2118-2221 |
358-379 |
C. Conclusion
The speed of connecting Java to MySQL is about twice the speed of PHP. Java uses the connection pool technology to significantly improve performance.
The performance of the persistent connection (pconnect) used by PHP is equivalent to the Java connection pool, and the result is incredible. I was worried that PHP would connect to the database because the connection pool is unavailable, I found some information on the Internet and did not find any available connection pool for PHP. I was surprised by the fact that many PHP programmers did not even hear this concept. However, in other words, the principle of pconnect is similar to that of the connection pool. The program closes the connection, but PHP does not actually close the connection. When you open the connection again, you can directly use the available connection. However, PHP implementation is indeed much more convenient than Java. It is easy to implement with just a letter. Java is a little more troublesome. It takes some time for me to configure the Tomcat connection pool, according to the official documents, the configuration was not successful in tomcat5.0.28. On the csdn page, many new users also found that many configurations were unsuccessful and finally found out by themselves, for the configuration method, refer to my blog.
If you move the number of connections to the database and shut down the database outside the loop, the execution time of PHP and JSP is almost the same, and the cycle is about 16 ms for 50 times. It can be seen that in database operations, opening a connection is quite resource-consuming. Other operations are slightly different from languages. When connecting to the database, JSP has much higher overhead than PHP. (Here, we specifically place open database operations in the loop body for comparison)
Iv. File Operations
In the web development process, file operations are generally indispensable. Most Web Systems upload or operate files.
A. Test Standards
In a comprehensive test of various file operations, the test process is to first determine whether a file exists, delete the file if it exists, create a new file, and then write a certain length of content to the file. The entire process repeats n times.
B. Test Results
Test code:
Test Type |
Test code |
JSP |
String filename = "jsp.txt "; String filepath = request. getrealpath (""); Int I = 0; Int J = 0; While (I <XXXX) { File F = new file (filepath, filename ); If (F. exists ()) { F. Delete (); } F. createnewfile (); Filewriter fw = new filewriter (filepath + file. Separator + filename ); J = 0; While (j <2 * I) { FW. Write (J + "\ r \ n "); J ++; } FW. Close (); I ++; } |
PHP |
$ Filename = "./php.txt "; $ I = 0; While ($ I <XXXX) { If (file_exists ($ filename )) { Unlink ($ filename ); } $ Handle = fopen ($ filename, "x "); $ J = 0; While ($ j <2 * $ I) { Fwrite ($ handle, $ J. "\ r \ n "); $ J ++; } Fclose ($ handle ); $ I ++; } |
Test result (unit: milliseconds)
XXXX Value |
JSP |
PHP |
100 |
265-292 (326,431) |
346-367 |
500 |
1703-2065 |
3256-3474 |
1000 |
4062-4398 |
9316-9348 |
C. Conclusion
This test result was a little surprising. I thought that PHP would win a lot, but JSP was far ahead. When the number of loops was large, PHP was twice slower than JSP. A little incredible.
V. Summary
In general, the performance of PHP and JSP cannot be very different, and each has its own advantages. Although JSP is compiled and executed, its performance will decrease through JVM. In terms of functions, JSP is relatively powerful, mainly because it supports Java. In some very large enterprises, JSP is advantageous (mainly supported by Java ), however, PHP can handle general enterprise or general applications. In addition, PHP is easier to use than JSP. In addition, for example, to build the same web system, PHP may be less complex than JSP (if all JSP technologies are used, the architecture environment is estimated, system design is troublesome. In addition, various Java technologies and products are relatively open and loose, and documents are relatively messy. At least I think Tomcat documents are not as well as Apache, PHP is good .).
PHP is small and flexible, and JSP is very favored. In addition, there are many concepts. I personally think that the java system is suspected of deliberately exaggerating the technical complexity.
In addition, PHP also has Zend available. I originally wanted to install Zend and test it again. In the past few days, Zend websites are always unable to get on. According to relevant tests, Zend can improve PHP performance by about 100%. Some may say 30-40%, or 600%. It is estimated that arithmetic operations can be upgraded to a certain level. Other operations can be upgraded to a maximum of 30-40%.
During the entire test process, I will test each value multiple times. Generally, I will test 10 operations under 10 s, and at least 3 operations over 10 s. PHP test results are relatively stable, and the difference between the results measured each time is not very large. From a statistical perspective, the variance is relatively small. In JSP, sometimes some values may deviate far from the average value. In addition, due to compilation, the first execution usually takes a long time (although the time difference between the first execution cannot be seen from the values displayed in the program, compilation takes some time before the program starts to execute. Execution starts after compilation, so the results displayed by the program cannot be seen .), Therefore, the result of the first running is not obtained in the JSP test.
In addition, during the database connection test, due to temporary negligence, I ++ in the loop body is forgotten, resulting in an endless loop. Therefore, several infinite loop tests were also conducted. In the arithmetic operation test, when the JSP is in an endless loop, the browser clicks the stop button, Tomcat continues to execute, and the CPU is always 100%. The endless loop can be stopped only when Tomcat is stopped in the task manager; PHP arithmetic operation dead loop, due to PhP. INI contains the maximum execution time and maximum memory. Therefore, an endless loop can automatically end. When the database is connected to an endless loop, an exception occurs after the JSP has been loop for more than 900 times. An error is reported, indicating that the connection fails. After the PHP loop for more than 1500 times, the connection still does not end, I had to end the Apache process (My PHP. INI sets the maximum PHP Execution time to 300 seconds ).
6. Simple testing in other environments
The above data can only be used as a reference. In the same program, I also did a simple test in other configuration systems:
I. P4 2.4g, 512 M DDR, Windows Server2000 Working Machine
(There are many installation items, which are messy and not reinstalled after running for more than a year. Because it is a working machine, the operating system optimizes the application, rather than the backend service. The measured data, it is much larger than the data measured in xp. For example, it takes about 500 ms to connect to MySQL for 16000 cycles in PHP .), The rough test result is:
Arithmetic Operations and string operations:Arithmetic Operations: JSP leads a lot, string operations, PHP leads a lot, consistent with the results of the test machine.
Connect to the database:The speed of connecting PHP and JSP to MySQL is not much different. php is about 20% faster. The strange thing is that I use pconnect to connect the same result, and the JSP connection pool is not tested. It is estimated that the JSP connection pool will be much faster.
File Operations:PHP file operations are about 30% better than JSP operations! The result is a little different from that of the test machine, leading PHP.
Ii. P4 2.66g, 1g DDR, Linux Server
Arithmetic Operations and string operations:The test results are consistent with those in other environments. jsp leads the way in arithmetic operations and PHP leads the way in string operations.
Connect to the database:Execute 500 cycles. php uses connect for 185 Ms. php uses pconnect for 70 ms. jsp does not use the connection pool for 2030 Ms.
File Operations:JSP leads a lot, with 1000 loops. jsp takes about ms, and PHP takes about Ms.
Iii. Summary
Regardless of the Platform System, the test results show that the performance gap between JSP and PHP in Web development is not big. We can only say that JSP has its own advantages. jsp is not as good as PHP in database operations and string operations, PHP is inferior to JSP in file operations and arithmetic operations.
In addition, the performance of PHP and JSP in Linux is superior to that in windows.