为什么不同的测试顺序会得到不同的结果?

发布于 2024-11-24 20:26:18 字数 1515 浏览 0 评论 0原文

我改变了测试顺序并得到了不同的结果。我尝试禁用操作码缓存,添加未设置,但仍然得到不同的结果。为什么 ?

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_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 ?

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_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍风燃霜 2024-12-01 20:26:18

我的想法是关于内存使用情况:我添加了 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

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:

Took 0.108744 seconds for array_push
memory: 32497848
Took 0.151069 seconds for array_push
memory: 320

and

Took 0.061715 seconds for array[]
memory: 32499584
Took 0.058831 seconds for array[]
memory: -40

so: array_push() seems not to be cleaning memory allocated for the script, and array[] seems to do so. php needs some time to allocate new memory (i guess), so array[] after array_push() doesn't need to spend time on it, but array_push() after array[] 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?! %|

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文