我改变了测试顺序,得到了不同的结果。我尝试禁用操作码缓存,添加未设置,但仍然得到不同的结果。为什么?
http://snipplr.com/view/759/$time_start = microtime(true);
$myArray = array();
for ( $i = 0; $i < 100000; ++$i )
{
$myArray[] = $i;
$myArray[] = 'test a string';
}
$time_end = microtime(true);
printf("Took %f seconds for array[]n", $time_end - $time_start);
$time_start = microtime(true);
$myArray = array();
for ( $i = 0; $i < 100000; ++$i )
{
array_push($myArray, $i);
array_push($myArray, 'test a string');
}
$time_end = microtime(true);
printf("Took %f seconds for array_pushn", $time_end - $time_start);
耗时0.145872秒for array[]耗时0.154502秒for array_push
$time_start = microtime(true);
$myArray = array();
for ( $i = 0; $i < 100000; ++$i )
{
array_push($myArray, $i);
array_push($myArray, 'test a string');
}
$time_end = microtime(true);
printf("Took %f seconds for array_pushn", $time_end - $time_start);
$time_start = microtime(true);
$myArray = array();
for ( $i = 0; $i < 100000; ++$i )
{
$myArray[] = $i;
$myArray[] = 'test a string';
}
$time_end = microtime(true);
printf("Took %f seconds for array[]n", $time_end - $time_start);
为array_push花了0.197076秒为array花了0.122565秒[]
将测试次数增加到500000:
for array[] for array_push
为array_push花了1.008018秒为array花了0.494230秒[]
看看我是否改变了测试顺序。2倍的速度差
我的想法是关于内存使用:我添加了memory_get_usage()
差异的回声(就像时间一样),看到了这个:
Took 0.108744 seconds for array_push
memory: 32497848
Took 0.151069 seconds for array_push
memory: 320
和
Took 0.061715 seconds for array[]
memory: 32499584
Took 0.058831 seconds for array[]
memory: -40
so: array_push()
似乎没有清理为脚本分配的内存,而array[]
似乎这样做了。php需要一些时间来分配新的内存(我猜),所以array[]
之后的array_push()
不需要花时间,但array_push()
之后的array[]
做。
或者,可能是谵妄
ps:所以,为了提高性能,必须调用一个消耗大量内存并且在脚本开始时不清理的函数?!% |