A-A+

php date()和sql FROM_UNIXTIME() 的效率比较

2017年10月23日 PHP技术文章 暂无评论 阅读 0 views 次

下面我们一起来看看php date()和sql FROM_UNIXTIME() 的效率比较吧,到底是那个的性能要好一些呢,一起看实例.

在php中,将int型的时间戳转换为日期时间,有两种方法,一种是用我们熟悉的函数date("Y-m-d H:i",time())来转换,还有一种是在sql中用 FROM_UNIXTIME(add_time, "%Y-%m-%d %H:%m") 转换,平时用的不多,估计很多人都还不知道吧.

为了了解他们之间的效率和区别,我做了一个实例,先建了一张表,只添加了两个字段,一个是自增长的id,一个是int型的时间,添加两条数据后,再用自我复制语句 insert into t1 (add_time) select add_time from t1 将表迅速的复制到10万多条数据,用于测试.

程序很简单,就是比较在mysql中转换和在php中转换执行时间比较.

每块代码我执行了很多遍,我从中取出的一个比较适中的时间,现在从下图可以很明显的看出在数据库中用 FROM_UNIXTIME() 函数比 php 的 date() 函数要高效的多,不过我们不能忽略mysql数据库执行的开销,所以在不考虑数据库开销的情况下 FROM_UNIXTIME() 是快速的.

PHP源码参考,代码如下:

  1. <?php   
  2.  header("Content-type:text/html;charset=utf-8");   
  3.  //程序运行时间   
  4.  $starttime = explode(' ',microtime());   
  5.    
  6.    
  7.  /*········以下是代码区·········*/   
  8.  $link = mysql_connect("localhost","root","root");   
  9.  mysql_select_db("test");   
  10.  mysql_query("set names utf8");   
  11.  $sql = "select add_time from t1 limit 100000";   
  12.  $res = mysql_query($sql,$link);   
  13.  $num = mysql_num_rows($res);   
  14.  while($row = mysql_fetch_array($res)){   
  15.   //echo date("Y-m-d H:i",$row['add_time'])." | ";   
  16.  }   
  17.  /*········以上是代码区·········*/   
  18.    
  19.    
  20.  //程序运行时间   
  21.  $endtime = explode(' ',microtime());   
  22.  $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);   
  23.  $thistime = round($thistime,3);   
  24.  echo '<hr/>本次通过 PHP 中 date("Y-m-d H:i",$row["add_time"]) 转换。<br/> 转换本次转换 '.$num.' 条数据。<br/>本次执行耗时:'.$thistime.' 秒。';   
  25.    
  26.    
  27.  //--------------------------------------------------------   
  28.  //--------------------------------------------------------   
  29.  //程序运行时间   
  30.  $starttime = explode(' ',microtime());   
  31.    
  32.    
  33.  /*········以下是代码区·········*/   
  34.  $sql = "select add_time from t1 limit 100000";   
  35.  $res = mysql_query($sql,$link);   
  36.  $num = mysql_num_rows($res);   
  37.  while($row = mysql_fetch_array($res)){   
  38.   echo $row['add_time']." | ";   
  39.  }   
  40.  /*········以上是代码区·········*/   
  41.    
  42.    
  43.  //程序运行时间   
  44.  $endtime = explode(' ',microtime());   
  45.  $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);   
  46.  $thistime = round($thistime,3);   
  47.  echo '<hr/>本次直接输出,没任何转换<br/>本次执行耗时:'.$thistime.' 秒。';   
  48.    
  49.    
  50.  //--------------------------------------------------------   
  51.  //--------------------------------------------------------   
  52.  //程序运行时间   
  53.  $starttime = explode(' ',microtime());   
  54.    
  55.    
  56.  /*········以下是代码区·········*/   
  57.  $sql = "select FROM_UNIXTIME( add_time, '%Y-%m-%d %H:%m' ) as add_time from t1 limit 100000";   
  58.  $res = mysql_query($sql,$link);   
  59.  $num = mysql_num_rows($res);   
  60.  while($row = mysql_fetch_array($res)){   
  61.   //echo $row['add_time']." | ";   
  62.  }   
  63.  /*········以上是代码区·········*/   
  64. //phpfensi.com   
  65.  //程序运行时间   
  66.  $endtime = explode(' ',microtime());   
  67.  $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);   
  68.  $thistime = round($thistime,3);   
  69.  echo '<hr/>本次通过 mysql 中 FROM_UNIXTIME( add_time, "%Y-%m-%d %H:%m" ) 转换。<br/> 转换本次转换 '.$num.' 条数据。<br/>本次执行耗时:'.$thistime.' 秒。';   
  70. ?>  
标签:

给我留言