PHP与Perl操作Memcached速度差异比较
由于最近在进行memcached方面的工作
在性能测试中
使用了php以及perl对memcached进行操作
结果发现php与perl对memcached操作的性能
差异大约在40~50%之间
以下是测试脚本
所作的操作一样.使用1k的数据重复512000次.
总共插入memcached 500M的数据
php操作脚本
<?php
ini_set("memcache.hash_function","crc32");
$memcache = new Memcache;
$memcache->addServer('localhost', 30001);
$memcache->flush();
for($i=0;$i<512000;$i++){
$memcache->set($i,
"共1k的填充数据",0,1000);
}
?>
ini_set("memcache.hash_function","crc32");
$memcache = new Memcache;
$memcache->addServer('localhost', 30001);
$memcache->flush();
for($i=0;$i<512000;$i++){
$memcache->set($i,
"共1k的填充数据",0,1000);
}
?>
接着是perl脚本
#!/usr/bin/perl
use Cache::Memcached();
$memcache=new Cache::Memcached{'servers'=>["localhost:30001"]};
$memcache->flush_all();
for($i=0;$i<512000;$i++){
$memcache->set($i,
"共1k的填充数据");
}
$memcache->disconnect_all();
use Cache::Memcached();
$memcache=new Cache::Memcached{'servers'=>["localhost:30001"]};
$memcache->flush_all();
for($i=0;$i<512000;$i++){
$memcache->set($i,
"共1k的填充数据");
}
$memcache->disconnect_all();
从代码行数上来看.两者也几近一致
但是测试结果却是大相径庭
我们在linux下使用time对执行进行计时
3次执行结果如下
[root@lenovo5 ~]# time ./test1k.pl
real 1m2.265s
user 0m36.427s
sys 0m17.114s
[root@lenovo5 ~]# time ./test1k.pl
real 1m2.814s
user 0m36.380s
sys 0m17.463s
[root@lenovo5 ~]# time ./test1k.pl
real 1m13.684s
user 0m44.603s
sys 0m18.366s
[root@lenovo5 ~]# time php ./test1k.php
real 0m38.055s
user 0m11.768s
sys 0m13.891s
[root@lenovo5 ~]# time php ./test1k.php
real 0m38.892s
user 0m12.416s
sys 0m14.044s
[root@lenovo5 ~]# time php ./test1k.php
real 0m38.955s
user 0m12.430s
sys 0m13.088s
real 1m2.265s
user 0m36.427s
sys 0m17.114s
[root@lenovo5 ~]# time ./test1k.pl
real 1m2.814s
user 0m36.380s
sys 0m17.463s
[root@lenovo5 ~]# time ./test1k.pl
real 1m13.684s
user 0m44.603s
sys 0m18.366s
[root@lenovo5 ~]# time php ./test1k.php
real 0m38.055s
user 0m11.768s
sys 0m13.891s
[root@lenovo5 ~]# time php ./test1k.php
real 0m38.892s
user 0m12.416s
sys 0m14.044s
[root@lenovo5 ~]# time php ./test1k.php
real 0m38.955s
user 0m12.430s
sys 0m13.088s
差异很明显.perl执行需要1分左右而php只需要40秒不到
就是php的执行比perl的大约快40%
分析之后有几个因素的可能
1.perl的字串处理速度较慢.我们看到perl版本的set中不需要加入长度参数.这样每次插入可能都会需要set函数去判断传入的字串长度.这样可能较慢.但是随后我们发现php的set虽然有长度参数.但是这个参数并非是强制性的.比如我参数写了1000,实际字串有1200.结果将会是插入1200长度的字串,而并没有截断.所以这一点不是非常站得住脚
2.perl的扩展与php扩展实现方式不同.php的memcache客户端是PECL.也就是C扩展,而perl的扩展实现很有可能还是perl.所以会有性能差异.
九月 26th, 2008 at 9:22 下午
Perl的字符串处理效率绝对比PHP高,别忘了Perl是从什么地方发展起来的
Perl的虚拟机执行效率也绝对比PHP要高
我认为问题出在Perl的Memcache客户端是用Perl写的
而PHP的是Memcache客户端是C语言写的PHP扩展
九月 27th, 2008 at 11:43 上午
比较认同楼上的观点。perl处理字符是一大特色…那是倍捧D。。。
九月 27th, 2008 at 9:51 下午
可能差距比40%还要大
因为php的循环的效率比较低,如果什么事情都不做
就只是做循环,可能php比perl要慢一些。
十月 1st, 2008 at 5:21 下午
Cache::Memcached::Fast
http://search.cpan.org/dist/Cache-Memcached-Fast/
这里有一个Perl的C Memcached客户端,楼主可以尝试一下然后再给一个结果
十一月 27th, 2008 at 11:56 下午
Cache::Memcached是pure perl的,速度慢那是肯定的。
我也做了个速度比较,结果是如果让perl也使用C库(使用模块Memcached::libmemcached),那么php和perl的实现速度不相上下。
代码和比较在这里:
http://www.surfybeach.net/blog/archives/165