以preFIX添加到数组键最快的方法? [英] Fastest way to add prefix to array keys?
问题描述
什么是字符串prefixes添加到数组键的最快方法?
输入
$阵列=阵列(
'1'=> VAL1,
'2'= GT; val2的,
);
所需的输出:
$阵列=阵列(
'prefix1'=> VAL1,
'prefix2'=> val2的,
);
我发现PHPBench不是不平凡的基准非常好的来源。所以,除非你的实际运行为(....)感兴趣;
它不会正确地显示哪些语法会更快。我已经把一个简单的基准表明,实际上的foreach是最快的,当你使用这两个键和值的迭代过程中。
实际上强制PHP从循环迭代读取的值是非常重要的,否则它会做最大努力把它优化了。在下面的例子中我使用了 doNothing
函数强制PHP来计算每次键和值。使用doNothing将导致额外开销要施加到每个循环的,但它会为每一个循环以来的呼叫数将是相同的是相同的。
我是不是真的感到惊讶,的foreach
技高一筹,因为它的语言构建迭代的字典。
$阵列=范围(0,1000000);功能doNothing(价值$,$键){;}$ t1_start = microtime中(真);
的foreach($数组$关键=> $值){
doNothing(价值$,$键);
}
$ t1_end = microtime中(真);$ t2_start = microtime中(真);
$ ARRAY_SIZE =计数($数组);
为($关键= 0; $键< $ ARRAY_SIZE; $键++){
doNothing($数组[$关键],$键);
}
$ t2_end = microtime中(真); //从PHPBench建议为最快的方式来遍历数组
$ t3_start = microtime中(真);
$键= array_keys($数组);
$大小=整型尺寸($键);
为($ I = 0; $ I< $大小; $ I ++){
doNothing($键[$ i],$阵列[$键[$ i]]);
}
$ t3_end = microtime中(真);$ t4_start = microtime中(真);
array_walk($阵列doNothing);
$ t4_end = microtime中(真);打印
测试1($ t1_end - t1_start $)。\\ n。 //测试1 0.342370986938
测试2($ t2_end - t2_start $)。\\ n。 //测试2 0.369848966599
实验3($ t3_end - t3_start $)。\\ n。 //测试3 0.78616809845
。测试4($ t4_end - $ t4_start)\\ n; //测试4 0.542922019958
编辑:我使用的PHP 5.3的64位MAC OSX 10.6
What is the fastest way to add string prefixes to array keys?
Input
$array = array(
'1' => 'val1',
'2' => 'val2',
);
Needed output:
$array = array(
'prefix1' => 'val1',
'prefix2' => 'val2',
);
I've found that PHPBench is not a very good source for non-trivial benchmarks. So unless your actually interested in running for(....);
it's not going to correctly show which syntax will be faster. I've put together a simple benchmark to show that foreach is actually the fastest when your use both the key and value during the iteration.
It's very important to actually force PHP to read the values from a loop iteration, or else it'll do its best to optimize them out. In the example below I use the doNothing
function to force PHP to calculate the key and value each time. Using doNothing will cause an overhead to be applied to each loop, but it will be the same for each loop since the number of calls will be the same.
I wasn't really that surprised that foreach
came out on top since it's the language construct for iterating a dictionary.
$array = range( 0, 1000000 );
function doNothing( $value, $key ) {;}
$t1_start = microtime(true);
foreach( $array as $key => $value ) {
doNothing( $value, $key );
}
$t1_end = microtime(true);
$t2_start = microtime(true);
$array_size = count( $array );
for( $key = 0; $key < $array_size; $key++ ) {
doNothing( $array[$key], $key );
}
$t2_end = microtime(true);
//suggestion from PHPBench as the "fastest" way to iterate an array
$t3_start = microtime(true);
$key = array_keys($array);
$size = sizeOf($key);
for( $i=0; $i < $size; $i++ ) {
doNothing( $key[$i], $array[$key[$i]] );
}
$t3_end = microtime(true);
$t4_start = microtime(true);
array_walk( $array, "doNothing" );
$t4_end = microtime(true);
print
"Test 1 ".($t1_end - $t1_start)."\n". //Test 1 0.342370986938
"Test 2 ".($t2_end - $t2_start)."\n". //Test 2 0.369848966599
"Test 3 ".($t3_end - $t3_start)."\n". //Test 3 0.78616809845
"Test 4 ".($t4_end - $t4_start)."\n"; //Test 4 0.542922019958
Edit: I'm using PHP 5.3 on 64-bit Mac OSX 10.6
这篇关于以preFIX添加到数组键最快的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!