A PHP database inserts a record of performance comparisons resulting from the problem.

Source: Internet
Author: User
Tags php database php mysql php mysql extension
I am a C #, and for some reason recently, also learning PHP.
Testing a piece of code today, it is very simple, is to write a row of records in the database. Grasping the execution time of this program raises a question: how Slow is PHP.

My PHP environment is phpstudy integrated environment, PHP version is 5.3.29, server with Nginx.
MySQL database has a table with users:
ID (self-increment column), name (varchar), Time (time)
My PHP Test code:
 This page takes time: ". $thistime." Seconds. ". Time (); function Request ($s) {if ($_request) {$p = $_request[$s];if ($p = =" ") {return" ";} Else{return $p;}} Elsereturn "";} function Getrand () {$a = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz_ () &^}{+=|?! @#$% "; $start = rand (0,strlen ($a)-1); $len = rand (1,strlen ($a)); $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: '. mysq L_error ());} Echo ' Connected 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 execution time was more than 1 seconds, found very slow.

And the same, I use C # to complete the code:
        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 time:" + seconds); } private static void Run () {string name = "Abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnop            QRS ";            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 beyond PHP.
Test results:



Can the contrast difference be so big?


Reply to discussion (solution)

The test machine is the same one, configured as win7,i3,5400 to drive

Test once, does not indicate any problems



Test of course not once, you can see, PHP code used a loop, I monitored more than 5,000 write, each write is in 1 seconds or so.

And the same, C #, lasts dozens of times, except for the first time about 0.15 seconds or so, the others are around 0.0001 seconds.

So not once, I want to ask why this is the problem with the code or the test environment.

Your loop is HTTP cycle, and the test is actually the response speed of the entire web system.
At least one set of non-database access pages is required for comparison

A few more machine tests, a comprehensive comparison

Comment out the results of the database execution: The execution time is the same, almost 0.






Can it be explained that PHP is a performance problem? Is the reason for Win 7?

Comment out the results of the database execution: The execution time is the same, almost 0.






Can it be explained that PHP is a performance problem? Is the reason for Win 7?



Change the code, please.

Php-q test file. php

Run straight to see the results

As it turns out, it still takes 1 seconds.

Slow down is normal, logic is clear. Explain and compile how to compare!!

Slow down is normal, logic is clear. Explain and compile how to compare!!



Does that mean that the performance difference between the two is because one compiles a script that interprets execution?

But even so, the difference is not so big.

Does it mean that PHP is fading ...

Does it mean that PHP is fading ...



Don't joke about that, I believe that now such a large Internet enterprise with PHP to develop high-concurrency applications, the performance of PHP is actually very impressive.

I'm trying to figure out why I'm testing this, whether it's a code problem or an environmental issue.

In any case, inserting a record is unlikely to take 1 seconds
In the test, the control group should have the equivalent conditions
The mysql.net only has a real physical connection operation when the database is first connected, which is equivalent to a long connection to the PHP MySQL extension
But when you test, PHP uses a short connection
As I said earlier, you are testing the time of the entire HTTP session. The non-MySQL factors should be planed before jumping to conclusions

Nginx + PHP is the way CGI works
You don't even know how C # works.

If you blame MySQL for the delay in the running environment, it's obviously unjust.

It's obvious.

change localhost to 127.0.0.1, because MySQL will go to a DNS resolution process, the server should also use IP for database connection, or in (Windows) My.ini inside the configuration can be modified.

Even if (currently) testing the entire HTTP period PHP is still stronger than. Net.

C # is debugged directly using VS2010 's own ASP. NET development server, with no additional content.
The previous attempt to pass the database execution code, again run, time-consuming and in C #, is 0 (or approximately equal to 0)
Plus the database execution code, one is 1 seconds more, one is 0.001 seconds, then this should be able to judge the problem in the link to access MySQL.

is PHP access to MySQL database a self-propelled driver? C # is a connection library that is downloaded directly from the official website.

It's obvious.

change localhost to 127.0.0.1, because MySQL will go to a DNS resolution process, the server should also use IP for database connection, or in (Windows) My.ini inside the configuration can be modified.

Even if (currently) testing the entire HTTP period PHP is still stronger than. Net.



This does not conform to logic, even if you want to go DNS parsing, will not PHP go, C # will not go, I have used the localhost, and there is no use of IP.

Whole HTTP period, PHP is stronger than. NET, this seems to be in conflict with my test.

Perhaps as Xuzuning said, it is my test method is not correct, but in a cursory view, this does not seem to be a big problem, I know that many large sites are now using Nginx as a container for PHP in use, and I test C # just use the VS2010 of the test server, There is a difference between the two itself.

This does not conform to logic, even if you want to go DNS parsing, will not PHP go, C # will not go, I have used the localhost, and there is no use of IP.

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

What you are talking about is: under the same machine, use "reasonable" method to get "reasonable" result.

Perhaps as Xuzuning said, it is my test method is not correct, but in a cursory view, this does not seem to be a big problem, I know that many large sites are now using Nginx as a container for PHP in use, and I test C # just use the VS2010 of the test server, There is a difference between the two itself.

don't you think it's a deviation?


This does not conform to logic, even if you want to go DNS parsing, will not PHP go, C # will not go, I have used the localhost, and there is no use of IP.

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

What you are talking about is: under the same machine, use "reasonable" method to get "reasonable" result.

Perhaps as Xuzuning said, it is my test method is not correct, but in a cursory view, this does not seem to be a big problem, I know that many large sites are now using Nginx as a container for PHP in use, and I test C # just use the VS2010 of the test server, There is a difference between the two itself.

don't you think it's a deviation?



I'm afraid you understand the mistake here, my problem is the same machine, different server environment, performance difference why so big? The big reason is the problem of the code or the environment, or the test method, I said those who use nginx as a container, this is how to run off?

Develop PHP application, do not toss on Windows frame, operating system please replace LINUX,LAMP architecture!!!
This PHP performance is the best.

Develop PHP application, do not toss on Windows frame, operating system please replace LINUX,LAMP architecture!!!
This PHP performance is the best.



I'm not getting started, it's just a learning phase and the environment in Windows is the easiest.

I asked you to change the code so that you can simply insert the data to test it. The second one, we don't have the environment for you to do the test, so we have no other results. Maybe you can change a computer and run out of different results, each other's database driver should not be the same. C # does it have its own database driver?

Finally, use mysqli to test ... MySQL has been eliminated.

I asked you to change the code so that you can simply insert the data to test it. The second one, we don't have the environment for you to do the test, so we have no other results. Maybe you can change a computer and run out of different results, each other's database driver should not be the same. C # does it have its own database driver?

Finally, use mysqli to test ... MySQL has been eliminated.



Try to put the mysqli, the result is the same, no change.

Forget it, knot, maybe PHP is not as powerful as you might think.

Landlord really funny, write a piece of code like this to compare 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.