|
楼主 |
发表于 2008-11-10 20:54:42
|
显示全部楼层
CI 访问MySQL 同 PHP 直接访问速度差70倍
数据库用 MySQL,分别用 CI 和 PHP 直接访问 MySQL 数据库,时间也相差70多倍
查询同样一个表,使用 CI 访问共耗时:0.028937101364136;使用 PHP 直接访问耗时:0.0035531520843506
代码如下:
PHP复制代码
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function RunTime_Begin ()
{
global $_starttime;
$_nowtime = explode(" ", microtime());
$_starttime = $_nowtime[1] + $_nowtime[0];
}
function RunTime_End ()
{
global $_starttime;
$_nowtime = explode(" ", microtime());
$_endtime = $_nowtime[1] + $_nowtime[0];
$_totaltime = $_endtime - $_starttime;
return $_totaltime;
}
class MysqlTest extends Controller {
function MysqlTest ()
{
parent ::Controller();
}
function index ()
{
RunTime_Begin ();
$db2 = $this->load->database('default', true);
echo "<hr>connect time = ".RunTime_End ()."<br>\n";
$query = $db2->query("select * from TCode");
echo "<hr>query time = ".RunTime_End ()."<br>\n";
$i = 0;
foreach ($query->result() as $row)
{
$i++;
if ($i>20)
break;
echo $row->FID . ' ';
echo $row->FCode . ' ';
echo $row->FVal . ' ';
echo "<br>\n";
}
echo "<hr>loop time = ".RunTime_End ()."<br>\n";
}
function mysql_test ()
{
runtime_begin ();
$cn = mysql_connect("127.0.0.1", "root", "root");
echo "<hr>connect time = ".RunTime_End ()."<br>\n";
$db = mysql_select_db("test", $cn);
$rs = mysql_query("select * from TCode");
echo "<hr>query time = ".RunTime_End ()."<br>\n";
$i = 0;
while( $row = mysql_fetch_assoc($rs))
{
$i++;
if ($i>20)
break;
echo $row['FID'] . ' ';
echo $row['FCode'] . ' ';
echo $row['FVal'] . ' ';
echo "<br>\n";
}
echo "<hr>loop time = ".RunTime_End ()."<br>\n";
mysql_close($cn);
}
}
?>
复制代码
index 执行结果如下:
<hr>connect time = 0.023476123809814<br>
<hr>query time = 0.028537034988403<br>
1 0001 USER01 <br>
2 0002 USER02 <br>
3 0003 USER03 <br>
4 0004 USER04 <br>
5 0005 USER05 <br>
6 0006 USER06 <br>
7 0007 USER07 <br>
8 0008 USER08 <br>
9 0009 USER09 <br>
10 0010 USER10 <br>
11 0011 USER11 <br>
12 0012 USER12 <br>
13 0013 USER13 <br>
14 0014 USER14 <br>
15 0015 USER15 <br>
16 0016 USER16 <br>
17 0017 USER17 <br>
18 0018 USER18 <br>
19 0019 USER19 <br>
20 0020 USER20 <br>
<hr>loop time = 0.028937101364136<br>
mysql_test 执行结果如下:
<hr>connect time = 0.0026230812072754<br>
<hr>query time = 0.0034041404724121<br>
1 0001 USER01 <br>
2 0002 USER02 <br>
3 0003 USER03 <br>
4 0004 USER04 <br>
5 0005 USER05 <br>
6 0006 USER06 <br>
7 0007 USER07 <br>
8 0008 USER08 <br>
9 0009 USER09 <br>
10 0010 USER10 <br>
11 0011 USER11 <br>
12 0012 USER12 <br>
13 0013 USER13 <br>
14 0014 USER14 <br>
15 0015 USER15 <br>
16 0016 USER16 <br>
17 0017 USER17 <br>
18 0018 USER18 <br>
19 0019 USER19 <br>
20 0020 USER20 <br>
<hr>loop time = 0.0035531520843506<br> |
|