jsp,php Detailed performance test

Source: Internet
Author: User
Tags pconnect php database stmt

A few days ago in the CU saw someone compare PHP and jsp,.net, the results show that PHP is much lower than jsp,.net performance. I think even if there is a gap, it should not be so big, so carefully test the performance gap between several. Since it's been useless for a long time. NET, there is no test. NET for the moment.

This test data is true, but only as a reference. You are welcome to discuss.
In addition to making comparisons, it can also be used by php,jsp programmers to optimize their own programs.

This test has been bleaching (IPADDR,BCOMCN) original, welcome reprint, and indicate the source. Question Feedback: admin.net (at) 163.com

Test environment:
Hardware: P4 2.4g,1g memory Software: windowsxp+apache2.0.54+php4.4.0+mysql4.1.14+tomcat5.0.28
Discussion Address: http://bbs.chinaunix.net/viewthread.php?tid=641073
I. Arithmetic operations A. Test criteria

Loop n times, and do integer self-addition operations.

B. Testing

Test code:

Test Type JSP int integer type JSP Long Integer type 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 a number or average of very many occurrences, and M (x, y) indicates an occasional occurrence of x, Y. The following is the same.

XXXX value JSP int integer type Jsplong Integral type Jsplong Integral type 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

The arithmetic operation of Java, the optimization is very good, the estimate is the direct corresponding CPU instruction, the numerical value is large, the arithmetic operation performance is very stable.
It is important to note that Long's operation time is longer than int (after all, the 32-bit system), so try not to use a long when using long, and int.

PHP is a weak type of variable, arithmetic operation is unsatisfactory, estimation is not directly corresponding to the arithmetic operation of the machine code.
The process of the PHP arithmetic operation is guessed as follows, before the operation needs to detect whether the variable is a numeric value, and to convert a non-numeric value into a value, for example, PHP can do such an operation: 100+ "Zhoutang", The result is equal to 100. It is the weak type language, plus the detection and conversion before the operation, which results in poor performance of arithmetic operations.
PHP single operation time is relatively stable, probably every 10,000 times the arithmetic plus time spent about 3.5MS

Different methods of operation also have an impact on performance.

Two. String operations A. Test criteria

String JOIN operation comparison.

B. Testing

Test code:

Test Type Jsp Php
Test code String str= "";
while (Str.length () < XXXX)
{
str+= "a";
}
$str = "";
while (strlen ($STR) < XXXX)
{
$str. = "a";
}

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 handled with objects, and it is clear that Java is far worse than PHP in the comparison of strings. (Because the string operation, very much to do the connection, here only to compare the connection operation, but also a rough comparison of the substring, the difference is not very large)

PHP string operations, estimated that many are directly called C's string function, so the efficiency is much higher.

In web development, the connection of strings is very frequent (including a lot of time to generate SQL strings), so in arithmetic and string operations two aspects, Java (JSP) Advantages are not obvious, with PHP strengths.

Three. Database query A. Test criteria

Loop n times do database open, query, close operation. The main JSP directly connected to the database, JSP connection pool data training, PHP directly linked database, PHP persistent database to do the 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.executeupdate (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.executeupdate (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

Normal connection, Java even MySQL speed is much slower than PHP a few times. Java uses connection pooling technology to improve performance significantly.

PHP uses the persistent connection (Pconnect) performance is equivalent to Java connection pool, the results are really incredible, I was very worried about PHP database, because no connection pool available, deliberately found some information on the Internet, did not find PHP available connection pool, the relevant references are very few, Many PHP programmers have not even heard of this concept, and the result is really a surprise to me. However, the pconnect principle, similar to the connection pool, is that the program closes the connection, but PHP does not actually close, and when opened again, uses the available connection directly. However, the implementation of PHP is really much more convenient than Java, just add a letter, it is easy to implement, Java is a bit troublesome, I configure the connection pool Tomcat took some time, according to official documents, Under the tomcat5.0.28 not configured successfully, on the csdn above, also see a lot of novice sent for help, a lot of configuration is unsuccessful, finally found out, the configuration method can refer to my blog.

If you connect the database and close the database, move to the outside of the loop, just PHP and JSP execution time is similar, loop 50 times about 16ms. Visible, the database operation, open connection is quite resource-intensive, other operations, and the language is not very different. JSP is much more expensive than PHP when connecting to a database. (The Open database operation is deliberately placed in the loop body for comparison)

Four. File operation

In the Web development process, file operations are generally indispensable, most web systems will upload or manipulate files.

A. Test criteria

Comprehensive testing of various file operations, the test process is to first determine whether a file exists, delete the file, then create a new file, and then write a certain length of content in the file. The entire process loops 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 is a bit unexpected, this thought PHP will win the project, but let JSP ahead of the Times, the number of cycles, PHP slower than JSP one times. It's a little weird.

Five. Summary

Overall, PHP and JSP in performance, the difference is not very far, each has advantages. JSP is compiled and executed, but through the JVM, performance degrades. In terms of function, JSP is relatively strong, mainly because there is Java support, in some very large enterprises, with the advantage of JSP (mainly Java support), but the general enterprise or general application, PHP can be fully dealt with. In addition, PHP is easier to get started than JSP. In addition, the individual said, to do the same web system, PHP relative to the JSP, the complexity may be lower (JSP all the technology used, the estimation of the architecture environment, system design is enough trouble, and the Java system of various technologies and products more open and loose, the document is relatively a bit messy, At least I don't think Tomcat's documentation is apache,php good. )。

PHP is small and flexible, the JSP looks great, and, the concept of a large number of individuals think that the Java system has deliberately exaggerated the complexity of the suspicion of technology.

In addition, PHP also has Zend usable, originally wanted to install Zend again to test some, these days Zend website always go up. According to the test, Zend can improve PHP performance by about 100% or so. There are also said 30-40%, also said 600%. It is estimated that arithmetic operations can improve the grade, other operations, up to a maximum of about 30-40%.

Throughout the test process, each value I will be measured several times, generally 10s below the operation, I measured at least 10 times, 10s above the operation, at least 3 times. PHP test results are relatively stable, the results of each measurement is not very large, from a statistical point of view, the variance is small. JSP relatively speaking, occasionally there will be some values, deviate from the average is far, and, because of the need for compilation, the first execution of the general time is longer (although the value displayed from the program does not show the time gap of the first execution, but before the program begins to execute, the compilation will take some time.) The program does not start executing until it is compiled, so the results are not visible. ), so, the JSP test, did not take the results of the first run.

In addition, in the process of testing connection database, because of a momentary negligence, forget in the circulation body i++, resulting in a dead loop. So, several dead cycle tests were also deliberately done. Arithmetic operation Test, JSP dead loop, the browser point the Stop button, Tomcat continues to execute, the CPU has been 100%, only in the task manager stopped Tomcat, only to stop the dead loop; PHP arithmetic operation dead Loop, Because the php.ini has the longest time and maximum memory set by the program execution, the dead loop can automatically end. Database connection dead Loop, JSP in the loop about 900 times after the exception, error, prompt unable to connect; PHP Loop 1500 times, still no end, had to end the Apache process in the Task Manager (my php.ini set PHP maximum execution time of 300 seconds).

Six. Other environment simple test

The above data can only be used as a reference, the same program, in other configuration of the system, I have also done a simple test:

I.P4 2.4g,512m ddr,windows server2000 's work machine

(Install things more, more chaotic, run for more than a year did not reinstall, because it is a work machine, so the operating system for the application optimization, rather than the back-end service optimization; the measured data is much larger than the data measured in XP, such as PHP connected to MySQL Loop 500 times, about 16000ms. ), the result of the rough test is:

arithmetic operations and string operations: arithmetic operations, JSP leading a lot, string operations, PHP lead a lot, and test machine results consistent.

To connect to a database: PHP and JSP even MySQL speed gap is not big, PHP about 20% or so, it is strange that I use Pconnect connect with Connect is the same result, did not test the JSP connection pool. It is much faster to estimate the JSP connection pool.

file operation: PHP file operation is about 30% higher than JSP performance! With the test machine results a little difference between the larger, PHP lead.

II.P4 2.66g,1g ddr,linux Server

arithmetic and string operations: consistent with other environmental test results, JSP leads in arithmetic operations, and PHP leads a lot in string operations.

Connect database: Execute 500 cycles, PHP with Connect, spend 185ms,php use pconnect connection, spend 70ms;jsp unused connection pool, spend 2030ms.

file operation: JSP lead a lot, 1000 cycles, JSP cost about 1500ms, PHP cost about 7000ms.

Iii. Summary

No matter what platform system, test results show that JSP and PHP in the Web development, the performance gap is not small, can only say the advantages, JSP in the database operation and string operation is not as good as PHP, and PHP in the file operation and arithmetic operation is, less than JSP.

In addition, the same language, on different platform performance is not the same, according to my test results show that Linux under the php,jsp performance is better than Windows.

Seven. Acquisition of uptime

Language How to get run time
Jsp

Long timestart=0;
Long timeend=0;
Timestart=calendar.getinstance (). Gettimeinmillis ();

Intermediate code

Timeend=calendar.getinstance (). Gettimeinmillis ();
Out.println ("Start time:" +timestart+ "milliseconds <br>");
Out.println ("End Time:" +timeend+ "milliseconds <br>");
Out.println ("Time Spent:" + (Timeend-timestart) + "msec <br>");

Php

$timeStart = 0;
$timeEnd = 0;
$timeStart =mictime ();

Intermediate code

$timeEnd =mictime ();
echo "Start time:" $timeStart. " Milliseconds <br> ";
echo "End time:" $timeEnd. " Milliseconds <br> ";
echo "Takes time:". ($timeEnd-$timeStart). " Milliseconds <br> ";

function Mictime ()
{
Because PHP support does not support long type, only 5 bits of seconds are taken, plus milliseconds. Time period comparison, the preceding can be ignored
List ($usec, $sec) = Explode ("", Microtime ());
$sec =substr ($sec,-5);
$usec =ceil ($usec *1000);
while (strlen ($usec) <3)
{
$usec = "0". $usec;
}
return $sec. $usec;
}

Test comparison with different frames: http://www.techempower.com/benchmarks/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.