对PHP采集数据提取核心函数的速度的测试与分析

From: http://www.h4cker.org/blog/index.php?action=show&id=39

By: ToToDoDo

对PHP采集数据提取核心函数的速度的测试与分析

由于程序需要,于是对PHP采集中的字符提取的核心部分进行了执行速度的测试。
测试了三种最常见的提取办法:

方法一:

CODE:[Copy to clipboard]
```

require "class.debug.php";

function getContent ( $sourceStr )
{
$content = strstr( $sourceStr, '形' );
$content = substr( $content, 0, strrpos( $content, '言' ) + strlen( '言' ) );

return $content;
}

$sourceStr = '拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论';

$debug = new Debug;

$debug->startTimer();

for( $i = 0; $i < 1000000; $i++ )
{
$returnStr = getContent( $sourceStr );
}

$timeInfo = $debug->endTimer();

echo $timeInfo;

 1---  
 2  
 3通过比较低级的字符操作函数进行提取.   
 4  
 5  
 6方法二:   
 7  
 8**CODE:** |  [Copy to clipboard]   
 9---|---  
10|  ```
11   
12  
13require "class.debug.php";   
14  
15function getContent ( $sourceStr )   
16{   
17$pattern = "/形(.*?)言/is";   
18preg_match_all( $pattern, $sourceStr, $result );   
19return $result[1][0];   
20}   
21  
22$sourceStr = '拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论';   
23  
24$debug = new Debug;   
25  
26$debug->startTimer();   
27  
28for( $i = 0; $i < 1000000; $i++ )   
29{   
30$returnStr = getContent( $sourceStr );   
31}   
32  
33$timeInfo = $debug->endTimer();   
34  
35echo $timeInfo;   
36  

使用一个简单的正则来提取.

方法三:

CODE:[Copy to clipboard]
```

require "class.debug.php";

function getContent ( $sourceStr )
{
$content = explode( '形', $sourceStr );
$content = explode( '言', $content[1] );

return $content[0];
}

$sourceStr = '拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论';

$debug = new Debug;

$debug->startTimer();

for( $i = 0; $i < 1000000; $i++ )
{
$returnStr = getContent( $sourceStr );
}

$timeInfo = $debug->endTimer();

echo $timeInfo;

 1---  
 2  
 3通过两次explode分裂字符串来提取.   
 4  
 5  
 6测试前我的观点是: 1 &gt; 2 &gt; 3   
 7  
 8在两台电脑上进行测试,每台测试了两次,结果如下:   
 9  
10  
11(1)17.32061   
12(2)26.81763   
13(3)17.53692   
14  
15(1)17.87291   
16(2)26.88415   
17(3)17.10972   
18  
19(1)11.30147   
20(2)20.25284   
21(3)11.54464   
22  
23(1)11.69471   
24(2)21.19316   
25(3)11.72613   
26  
27So,最终结果不是我想的那样,第一种和第三种方法的速度相当.第二种方法消耗时间大约是第一,三种的两倍.   
28  
29看来正则由于匹配的原因速度是最慢的,而explode由于两次的分裂,虽然速度上不慢,但是资源消耗比第一种方法多,毕竟是分裂了两次,都是分裂到数组,开销比纯粹的简单字符函数处理要来得大.   
30  
31经由缺氧的爱提醒,对更多的字符内容进行测试...测试了im286.com的首页:   
32  
33**CODE:** |  [Copy to clipboard]   
34---|---  
35|  $sourceStr = implode ('', file ('files.html'));   
36---  
37  
38(因为考虑到网络不够稳定,先把html文件保存到了本地)   
39  
40  
41最后的测试结果为:   
42  
43(1)5.61648   
44(2)13.4523   
45(3)22.56853   
46  
47分裂的效率是最低的...    
48综上,推荐使用方法一.   
49  
50有兴趣的朋友可以自己测试下:   
51  
52class.debug.php   
53  
54**CODE:** |  [Copy to clipboard]   
55---|---  
56|  ```
57   
58  
59class Debug   
60{   
61function startTimer()   
62{   
63global $starttime;   
64$mtime = microtime ();   
65$mtime = explode (' ', $mtime);   
66$mtime = $mtime[1] + $mtime[0];   
67$starttime = $mtime;   
68}   
69  
70function endTimer()   
71{   
72global $starttime;   
73$mtime = microtime ();   
74$mtime = explode (' ', $mtime);   
75$mtime = $mtime[1] + $mtime[0];   
76$endtime = $mtime;   
77$totaltime = round (( $endtime - $starttime), 5);   
78return $totaltime;   
79}   
80}   

如果您有更好的办法希望不吝赐教

Published At
Categories with Web编程
Tagged with
comments powered by Disqus