Efficiency Comparison of PHP date () and SQL From_unixtime ()

Source: Internet
Author: User

In PHP, there are two ways to convert the timestamp of int to datetime, one with the familiar function date ("Y-m-d h:i", Time ()), and the other is to use From_unixtime in SQL (Add_time, "%y-%m-%d %h:%m ") conversion, usually used not much, it is estimated that a lot of people do not know it.

In order to understand the efficiency and difference between them, I made an example. First built a table, only added two fields, one from the growth ID, one is int type of time, add two data, then use the self-replicating statement insert into T1 (add_time) Select add_time from T1 quickly copy the table to more than 100,000 data , for testing.


The program is simple, comparing the conversion in MySQL with the conversion execution time in PHP.

Each piece of code I perform many times, I take out a relatively modest time, now from the following figure it is obvious that the from_unixtime () function in the database is more efficient than the PHP date () function, but we cannot ignore the cost of MySQL database execution. So From_unixtime () is fast without considering the cost of the database.


PHP Source Reference:

The code is as follows Copy Code

<?php

Header ("Content-type:text/html;charset=utf-8");

Program Run time

$starttime = Explode (', microtime ());


/*········ The following is the code area */

$link = mysql_connect ("localhost", "root", "root");

mysql_select_db ("test");

mysql_query ("Set names UTF8");

$sql = "Select add_time from T1 limit 100000";

$res = mysql_query ($sql, $link);

$num = mysql_num_rows ($res);

while ($row = Mysql_fetch_array ($res)) {

echo Date ("Y-m-d h:i", $row [' Add_time ']). "| ";

}

/*········ Above is code area */


Program Run time

$endtime = Explode (', microtime ());

$thistime = $endtime [0]+ $endtime [1]-($starttime [0]+ $starttime [1]);

$thistime = Round ($thistime, 3);

Echo


//--------------------------------------------------------

//--------------------------------------------------------

Program Run time

$starttime = Explode (', microtime ());


/*········ The following is the code area */

$sql = "Select add_time from T1 limit 100000";

$res = mysql_query ($sql, $link);

$num = mysql_num_rows ($res);

while ($row = Mysql_fetch_array ($res)) {

echo $row [' Add_time ']. | ";

}

/*········ Above is code area */


Program Run time

$endtime = Explode (', microtime ());

$thistime = $endtime [0]+ $endtime [1]-($starttime [0]+ $starttime [1]);

$thistime = Round ($thistime, 3);

Echo '


//--------------------------------------------------------

//--------------------------------------------------------

Program Run time

$starttime = Explode (', microtime ());


/*········ The following is the code area */

$sql = "Select From_unixtime (add_time, '%y-%m-%d%h:%m ') as add_time from T1 100000";

$res = mysql_query ($sql, $link);

$num = mysql_num_rows ($res);

while ($row = Mysql_fetch_array ($res)) {

echo $row [' Add_time ']. | ";

}

/*········ Above is code area */


Program Run time

$endtime = Explode (', microtime ());

$thistime = $endtime [0]+ $endtime [1]-($starttime [0]+ $starttime [1]);

$thistime = Round ($thistime, 3);

Echo

?>

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.