为什么不同的测试顺序会得到不同的结果?
我改变了测试顺序并得到了不同的结果。我尝试禁用操作码缓存,添加未设置,但仍然得到不同的结果。为什么 ?
$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_push\n", $time_end - $time_start);
数组花费了 0.145872 秒[] 花费了 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_push\n", $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);
花费了 0.197076 秒 for array_push 花费了 0.122565 秒 for array[]
增加测试数量到 500000:
array[] 花费了 0.779719 秒 array_push 花费了 0.757806 秒
array_push 花费了 1.008018 秒 array[] 花费了 0.494230 秒
看看我是否更改测试顺序。这是 2 倍的速度差异。
I changed the test order and get different result. I tried disable opcode cache, added unset, but still get different result. Why ?
$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_push\n", $time_end - $time_start);
Took 0.145872 seconds for array[] Took 0.154502 seconds 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_push\n", $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);
Took 0.197076 seconds for array_push Took 0.122565 seconds for array[]
Increase test number to 500000:
Took 0.779719 seconds for array[] Took 0.757806 seconds for array_push
Took 1.008018 seconds for array_push Took 0.494230 seconds for array[]
See if I change test order. it's 2X speed difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的想法是关于内存使用情况:我添加了
memory_get_usage()
差异的回显(就像时间一样)并看到了这个:所以
:
array_push()
似乎不是清理为脚本分配的内存,而 array[] 似乎就是这样做的。 php 需要一些时间来分配新的内存(我猜),所以在array_push()
之后的array[]
不需要花时间在上面,但是array_push ()
之后array[]
执行。或者,也许这是一种谵妄
ps:所以,为了提高性能,必须调用一个消耗大量内存并且不会在脚本开始时清理的函数?! %|
my thought is about memory usage: i added echo of difference of
memory_get_usage()
(just like the time) and saw this:and
so:
array_push()
seems not to be cleaning memory allocated for the script, andarray[]
seems to do so. php needs some time to allocate new memory (i guess), soarray[]
afterarray_push()
doesn't need to spend time on it, butarray_push()
afterarray[]
does.or, maybe it's a delirium
ps: so, to increase performance one has to call a function which consumes much memory and doesn't clean up at the beginning of the script?! %|