现在的位置: 首页 > 综合 > 正文

PHP 读写TXT与Mysql性能测试

2012年06月14日 ⁄ 综合 ⁄ 共 4277字 ⁄ 字号 评论关闭

      最近设计了一个手机短信投票系统,存储方式没有使用数据库,而是使用了文本文档. 使用文本来存储数据,可以使用自己设计的数据结构,很爽。 虽然关于二者性能的优劣早有定论,不过心里总是对性能有点不放心,于是做了个对文本和数据库的数据的读取和写入测试……。結果如下:(测试程序的源码在后面)

Date Write 1 times by txt in 0.000149965286255 seconds.         写入文本文档
Date Write 
1 times by Mysql in 0.000132083892822 seconds.    写入数据库
Date Write 
1 times by Mysql in 8.41617584229E-5 seconds.       写入数据库,每次写完断开连接
Date Read 
1 times by txt in 4.38690185547E-5 seconds.        
Date Read 
1 times by Mysql in 4.79221343994E-5 seconds. 
Date Read 
1 times by Mysql in 7.79628753662E-5 seconds.

Date Write 100 times by txt in 0.0311291217804 seconds.
Date Write 
100 times by Mysql in 0.00491619110107 seconds.
Date Write 
100 times by Mysql in 0.00716996192932 seconds.
Date Read 
100 times by txt in 0.00154209136963 seconds.
Date Read 
100 times by Mysql in 0.00548791885376 seconds.
Date Read 
100 times by Mysql in 0.00764584541321 seconds.

Date Write 1000 times by txt in 0.440738916397 seconds.
Date Write 
1000 times by Mysql in 0.153868198395 seconds.
Date Write 
1000 times by Mysql in 0.0728650093079 seconds.
Date Read 
1000 times by txt in 0.0153751373291 seconds.
Date Read 
1000 times by Mysql in 0.0554130077362 seconds.
Date Read 
1000 times by Mysql in 0.0771050453186 seconds.

Date Write 10000 times by txt in 4.2115380764 seconds.
Date Write 
10000 times by Mysql in 0.572541952133 seconds.
Date Write 
10000 times by Mysql in 0.734919071198 seconds.
Date Read 
10000 times by txt in 0.131312847137 seconds.
Date Read 
10000 times by Mysql in 0.503059864044 seconds.
Date Read 
10000 times by Mysql in 0.720898151398 seconds.

Date Write 100000 times by txt in 42.7511901855 seconds.
Date Write 
100000 times by Mysql in 5.04986691475 seconds.
Date Write 
100000 times by Mysql in 7.31053590775 seconds.
Date Read 
100000 times by txt in 1.18301200867 seconds.
Date Read 
100000 times by Mysql in 4.85254502296 seconds.
Date Read 
100000 times by Mysql in 7.2050151825 seconds.

 

 

 

由上面可以看出,对于数据的读操作,文本文档的优势非常的明显,基本上10万次的读取都可以在2秒内完成,而数据库的普通的表可能要花上近5秒的时间,但是写操作就完全不同了,写入文本文档10万次需要40多秒,而数据库为5秒多一点……

     结论是:如果写操作不多,读操作很多,最好是用文本; 写操作多,读操作少,数据库较佳;……那么读写操作都很频繁呢,呵呵,如果是网站信息的话,目前的解决方案应该是缓存,但是还经引出缓存更新的问题,在这儿就不说了。

       对了,测试程序源码:

 


<?php
/**
 * @filename: test_txt_mysql.php
 * @touch date: 27/6/2009 23:41:45
 * @author: javazyf@gmail.com
 * @version: 1.0.0 
 
*/

//测试的次数
$times = 1;
print_r("\n");

//写入文本文件$times次测试
$starttimer = microtime(true);
for($o = 0 ; $o < $times$o++){
    
file_put_contents('re.txt',$o);
}
$stoptimer = microtime(true);

$timer = $stoptimer-$starttimer;
echo "Date Write $times times by txt in $timer seconds.\n";

 //写入数据库$times次测试-不关闭数据库连接
$conn = mysql_connect("localhost", "root","");
mysql_select_db("test");

$starttimer = microtime(true);
for($o = 0 ; $o < $times$o++){
    
$exec = "update test_txt set zyf=$o";
    
mysql_query($exec);
}
$stoptimer = microtime(true);

$timer = $stoptimer-$starttimer;
echo "Date Write $times times by Mysql in $timer seconds.\n";

 //写入数据库$times次测试-每次使用单独的数据库连接

$starttimer = microtime(true);
for($o = 0 ; $o < $times$o++){
    
$conn = mysql_connect("localhost", "root","");
    
mysql_select_db("test");
    
$exec = "update test_txt set zyf=$o";
    
mysql_query($exec);
    
mysql_close();
}
$stoptimer = microtime(true);

$timer = $stoptimer-$starttimer;
echo "Date Write $times times by Mysql in $timer seconds.\n";

//====================================================================

//读取文本文件$times次测试
$starttimer = microtime(true);
for($o = 0 ; $o < $times$o++){
    
file_get_contents('re.txt');
}
$stoptimer = microtime(true);

$timer = $stoptimer-$starttimer;
echo "Date Read $times times by txt in $timer seconds.\n";

 //读取数据库$times次测试-不关闭数据库连接
$conn = mysql_connect("localhost", "root","");
mysql_select_db("test");

$starttimer = microtime(true);
for($o = 0 ; $o < $times$o++){
    
$exec = "update test_txt set zyf=$o";
    
mysql_query($exec);
}
$stoptimer = microtime(true);

$timer = $stoptimer-$starttimer;
echo "Date Read $times times by Mysql in $timer seconds.\n";

 //读取数据库$times次测试-每次使用单独的数据库连接

$starttimer = microtime(true);
for($o = 0 ; $o < $times$o++){
    
$conn = mysql_connect("localhost", "root","");
    
mysql_select_db("test");
    
    
$exec = "update test_txt set zyf=$o";
    
mysql_query($exec);
    
mysql_close();
}
$stoptimer = microtime(true);

$timer = $stoptimer-$starttimer;
echo "Date Read $times times by Mysql in $timer seconds.\n";
print_r("\n");
?>

 

抱歉!评论已关闭.