Performance Comparison of insert records in a php database.

Source: Internet
Author: User
Tags php mysql php mysql extension
Performance Comparison of insert records in a php database. I am engaged in C #. recently, for some reasons, I am also learning php.
I tested a piece of code today. it is very easy to write a row of records into the database. Taking a look at the execution time of this program generates a question: how is php so slow.

My php environment is the phpstudy integrated environment, and the php version is 5.3.29. nginx is used for the server.
Mysql database has a table users:
Id (auto-incrementing column), name (varchar), time (time)
My php test code:
Script function r (s) {window. location = s;} script
 Time consumed on this webpage: ". $ thistime." seconds. ". Time (); function Request ($ s) {if ($ _ REQUEST) {$ p = $ _ REQUEST [$ s]; if ($ p = "") {return "" ;}else {return $ p ;}} elsereturn "" ;}function getRand () {$ a = "response _() & ^} {+ = | ?! @ # $ % "; $ Start = rand (0, strlen ($ a)-1); $ len = rand (1, strlen ($ )); $ str = substr ($ a, $ start, $ len); echo "generated user name :". $ str."
"; Return $ str;} function InsertRecord ($ s) {$ link = mysql_connect ('localhost', 'root', 'root'); if (! $ Link) {die ('could not connect :'. mysql_error ();} // echo 'Ted CTED successfully '; $ SQL = "insert into users (name, time) values ('". $ s. "','". date ('Y-m-d h: I: S', time ()). "')"; // execute SQL query mysql_select_db ("test", $ link); // Set the database to be operated // $ result = mysql_query ($ SQL, $ link); if (! Mysql_query ($ SQL, $ link) {die ('Error :'. mysql_error ();} mysql_close ($ link); // echo "1 record added" ;}?>


I found that the execution time was more than 1 second, and it was very slow.

In the same way, I use C # to complete the code section:
Protected void Page_Load (object sender, EventArgs e) {DateTime start = DateTime. now; Run (); DateTime end = DateTime. now; TimeSpan span = end-start; double seconds = span. totalSeconds; Response. write ("Program duration:" + seconds);} private static void Run () {string name = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRS"; Console. writeLine ("insert:" + name); MySqlConnection conn = new MySqlConnection (); conn. connectionString = "server = localhost; User Id = root; password = root; Database = test"; conn. open (); MySqlCommand cmd = new MySqlCommand (); cmd. connection = conn; cmd. commandText = "insert into users (name, time) values ('" + name + "', '" + DateTime. now. toString () + "')"; cmd. executeNonQuery (); cmd. dispose (); conn. close (); conn. dispose ();}

The result is that the speed is far higher than that of php.
Test results:



Can the difference be so big?


Reply to discussion (solution)

The same server is used for testing and is configured as win7, i3, 5400 to hard drive.

Test once, no problem is described



Of course, the test is not once. we can see that php code uses a loop. I have monitored more than 5000 writes, and each write is in about 1 second.

In the same way, c # lasted dozens of times, except for about 0.15 seconds for the first time, all others were about 0.0001 seconds.

This is not the case once. I want to ask why, whether there is a code problem or a test environment problem.

Your cycle is an http large loop, and the actual test is the response speed of the entire web system.
At least one group of non-database access pages is required for comparison.

Replace multiple machines for testing.

Comment out the database execution result: the execution time is the same, almost 0.






Can this be explained as a php performance problem? Is it because of win 7?

Comment out the database execution result: the execution time is the same, almost 0.






Can this be explained as a php performance problem? Is it because of win 7?



Change the code

Php-q test file. php

Run the command to view the result.

The result is the same. it still takes 1 second.

It is normal to slow down and the logic is clear. Explain how to compare with compilation !!

It is normal to slow down and the logic is clear. Explain how to compare with compilation !!



Does it mean that the performance difference between the two is caused by a compilation of a script for interpretation and execution?

But even so, the difference will not be so big.

It's hard to say that PHP has declined ......

It's hard to say that PHP has declined ......



Don't joke about it. I believe that such a large Internet enterprise is developing highly concurrent applications with php, which shows that php's performance is actually very impressive.

I want to figure out why I tested it like this, whether it is a code issue or an environment issue.

In any case, inserting a record cannot take 1 second.
During the test, the control group should have equal conditions
Mysql.net only has physical connection operations when connecting to the database for the first time, which is equivalent to the persistent connection of php mysql extension.
But when you test, php uses short connections.
As I mentioned earlier, you are testing the time of the entire http session. Non-MySQL factors should be split before conclusion

Nginx + php is the CGI working method
You have not explained how C # Works.

If you blame MySQL for the latency caused by the running environment, it is obviously unfair.

Obviously

Replace localhost with 127.0.0.1Because MySQL will go through a DNS resolution process, you should also use ip addresses on the server to connect to the database, or modify the configuration in my. ini (windows.

Even if the (current) testing of the entire http cycle PHP is still stronger than. net.

C # is directly debugged using the asp.net Development server provided by VS2010 without additional content.
The front side tries to drop the code executed by the database and run it again. The time consumed is the same as that in C #, and both are 0 (or approximately equal to 0)
After the database executes the code, one is more than one second, and the other is 0.001 seconds. then, we can determine that the problem lies in accessing mysql.

Is php a built-in driver for accessing the mysql database? C # is the connection library directly downloaded from the official website.

Obviously

Replace localhost with 127.0.0.1Because MySQL will go through a DNS resolution process, you should also use ip addresses on the server to connect to the database, or modify the configuration in my. ini (windows.

Even if the (current) testing of the entire http cycle PHP is still stronger than. net.



This is not in line with the logic. even if you want to use dns resolution, you will never go to php. c # will not go. I use localhost, and there is no ip address in place.

Php is better than. net throughout the http cycle. This seems to be in conflict with my test.

Maybe, as xuzuning said, my test method is incorrect, but in a rough view, this does not seem to be a big problem, all I know is that many large websites are currently using nginx as a php container, while all I test C # is the test server that comes with VS2010, there is a difference between the two.

This is not in line with the logic. even if you want to use dns resolution, you will never go to php. c # will not go. I use localhost, and there is no ip address in place.

I do not allow you to use 127.0.0.1 for both PHP and. NET.

The content you discuss is:Use "reasonable" methods to get "reasonable" results on the same machine.

Maybe, as xuzuning said, my test method is incorrect, but in a rough view, this does not seem to be a big problem, all I know is that many large websites are currently using nginx as a php container, while all I test C # is the test server that comes with VS2010, there is a difference between the two.

Don't you think it's a false positive?


This is not in line with the logic. even if you want to use dns resolution, you will never go to php. c # will not go. I use localhost, and there is no ip address in place.

I do not allow you to use 127.0.0.1 for both PHP and. NET.

The content you discuss is:Use "reasonable" methods to get "reasonable" results on the same machine.

Maybe, as xuzuning said, my test method is incorrect, but in a rough view, this does not seem to be a big problem, all I know is that many large websites are currently using nginx as a php container, while all I test C # is the test server that comes with VS2010, there is a difference between the two.

Don't you think it's a false positive?



I'm afraid you have misunderstood this. my problem is that the performance of the same machine varies with the server environment. why is the performance difference so big? The major cause is the code, environment, or test method. I have mentioned that nginx is used as a container. how can this be a problem?

Do not develop php applications on a windows framework. change the operating system to a linux or lamp architecture !!!
This php performance is the best.

Do not develop php applications on a windows framework. change the operating system to a linux or lamp architecture !!!
This php performance is the best.



I have not started yet, but at the learning stage, the windows environment is the easiest to deploy.

I want you to change the code to simply insert data for testing. next, we didn't test your environment for the moment, so we couldn't get any other results. maybe you could change your computer and run different results, each Other's database drivers should not be the same. C # Does it have their own database drivers.

Finally, we need to use mysqli to test... mysql has been eliminated.

I want you to change the code to simply insert data for testing. next, we didn't test your environment for the moment, so we couldn't get any other results. maybe you could change your computer and run different results, each Other's database drivers should not be the same. C # Does it have their own database drivers.

Finally, we need to use mysqli to test... mysql has been eliminated.



I tried mysqli and the results were the same, but they did not change.

Forget it. maybe php is not as powerful as you think.

This is really funny. after writing such code, we can compare the performance.

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.