php的hashtable和C++的vector,map比较

最近公司流行性能测试比较,于是大家把php,python,C,C++都比较了一下,
发现还是php<python<C++<C,注意C++使用STL里实现,可能大家认为都是这样的。
今天我又自己写了一个php的内建函数来测试下在php内部的性能,这个测试只比较了
插入速度,有一定的局限性,所以只是好玩。值得注意的如果用C/C++调用zend hash
实现和php代码实现之间也有一定的提升,好玩的是C/C++assoc比index要快不少,
数据仅供参考。在使用estrdup()做字符串拷贝后果然时间就差不多了,
虚拟机里的linux测试数据是

php运行结果:第一个index,第二个assoc

0.70816087722778
1.1056010723114

0.702232837677
1.1154210567474

0.71842908859253
1.1834888458252

php代码:

$start = microtime(1);
$array1 = array();
for($a=0;$a<1000000;$a++)
{
$array1[] = $a;
}
//print_r($array1);
echo microtime(1)-$start;
echo "
";
$start = microtime(1);
$array2 = array();
for($a=0;$a<1000000;$a++)
{
//strval($a);
$array2[(string)$a]=$a;
}
echo microtime(1)-$start;

C++运行结果
使用STL结果:第一个vector,第二个map

0.02657413482666
0.31584000587463
-----------------------------
0.010542869567871
0.3225519657135
--------------------------
0.027655124664307
0.3136420249939

zval API结果:第一个index,第二个assoc
0.63842296600342
0.40574407577515
---------------------------
0.59853315353394
0.4173080921173
-------------------------
0.6196460723877
0.4032609462738

C++代码:

PHP_FUNCTION(testPerformance)
{
long count;
char *index_type = NULL;
int index_type_len;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"ls", &count, &index_type, &index_type_len) == FAILURE) {
RETURN_NULL();
}
if((strcmp(index_type,"assoc")!=0)&&(strcmp(index_type,"index")!=0))
{
php_printf("param 2 must assoc or index");
RETURN_NULL();
}
/*
//zval 测试
array_init(return_value);

if(strcmp(index_type,"index")==0)
{
//add_index_long(return_value,0,0);
//已经插入了第一条数据
for(long i=1; i
{
add_index_long(return_value,i,i);
//add_next_index_long(return_value,i);
}

}else{
for(long i=0; i
{
char buffer[65];
sprintf(buffer,"%i",count);
add_assoc_long(return_value,buffer,i);
}
}
*/
//C++插入测试
if(strcmp(index_type,"index")==0)
{
vector v;
for(long i=0; i
{
v.push_back(i);
}

}else{
map m;
for(long i=0; i
{

char buffer[65];
sprintf(buffer,"%i",count);
m.insert(pair(buffer,i));
}
}

}