哪个部分花费更多时间,使用函数还是使用全局?
Which part takes more time, using function or using global?
你能告诉我为什么第二个代码 运行 比第一个代码快两倍(6 秒和 11 秒)(在所有 php 版本中)吗?原因是使用函数或使用全局或其他任何东西,为什么?我想在其他脚本中防止这个错误,但我不知道我的错误到底是什么。
我运行这个脚本用在线工具,结果一样。
配置文件第一个代码:
第一个代码:
for ($i = 1; $i < 2500; ++$i) {
$pen[$i] = $i * (3 * $i - 1 ) / 2;
}
function pentagonal($num) {
global $pen;
return $pen[$num];
}
function is_pentagonal($c) {
$x = (1+sqrt(1+24*$c))/(6);
if ($x == (int)$x) {
return true;
} else {
return false;
}
}
for ($i = 2; ; ++$i) {
for ($j = 1; $j < $i ; ++$j) {
$pi = pentagonal($i); // Here is the difference
$pj = pentagonal($j); // Here is the difference
if (is_pentagonal($pi + $pj, $pen)) {
if (is_pentagonal(abs($pi - $pj), $pen)) {
$difference = $pi - $pj;
break 2;
}
}
}
}
echo $i.' '.$j.' '.$difference."\n";
第二个代码(只是删除函数并直接从数组中获取值):
for ($i = 1; $i < 2500; ++$i) {
$pen[$i] = $i * (3 * $i - 1 ) / 2;
}
// function pentagonal($num) {
// global $pen;
// return $pen[$num];
// }
function is_pentagonal($c) {
$x = (1+sqrt(1+24*$c))/(6);
if ($x == (int)$x) {
return true;
} else {
return false;
}
}
for ($i = 2; ; ++$i) {
for ($j = 1; $j < $i ; ++$j) {
$pi = $pen[$i]; // Here is the difference
$pj = $pen[$j]; // Here is the difference
if (is_pentagonal($pi + $pj, $pen)) {
if (is_pentagonal(abs($pi - $pj), $pen)) {
$difference = $pi - $pj;
break 2;
}
}
}
}
echo $i.' '.$j.' '.$difference."\n";
在动态语言中查找全局变量或其他变量通常实现为 table 查找(散列查找或偏移量)。这是非常快的。函数调用总是 "expensive"... 需要执行一定数量的设置和拆卸代码,这可以转换为数千条机器代码指令。相比之下,这很慢。
尽管如此,在更大的系统中用直接变量访问替换所有函数调用是愚蠢的。如果您的问题完全在上面的代码中表达,那么是的,直接访问变量,并在完成后丢弃代码。
如果这是更大系统的一部分,请使用函数。它使测试、调试、静态分析、分析、一切……变得容易得多。即使代码的第一个变体比第二个变体快两倍,它也几乎会淹没在正在进行的所有其他事情的噪音中,尤其是在发生任何 IO 时。
更新:您可以将函数表示为...
function is_pentagonal($c) {
$x = (1+sqrt(1+24*$c))/(6);
return $x == (int)$x;
}
... 因为这将避免创建词法作用域(或在 PHP 中调用的任何内容)。
你能告诉我为什么第二个代码 运行 比第一个代码快两倍(6 秒和 11 秒)(在所有 php 版本中)吗?原因是使用函数或使用全局或其他任何东西,为什么?我想在其他脚本中防止这个错误,但我不知道我的错误到底是什么。
我运行这个脚本用在线工具,结果一样。
配置文件第一个代码:
for ($i = 1; $i < 2500; ++$i) {
$pen[$i] = $i * (3 * $i - 1 ) / 2;
}
function pentagonal($num) {
global $pen;
return $pen[$num];
}
function is_pentagonal($c) {
$x = (1+sqrt(1+24*$c))/(6);
if ($x == (int)$x) {
return true;
} else {
return false;
}
}
for ($i = 2; ; ++$i) {
for ($j = 1; $j < $i ; ++$j) {
$pi = pentagonal($i); // Here is the difference
$pj = pentagonal($j); // Here is the difference
if (is_pentagonal($pi + $pj, $pen)) {
if (is_pentagonal(abs($pi - $pj), $pen)) {
$difference = $pi - $pj;
break 2;
}
}
}
}
echo $i.' '.$j.' '.$difference."\n";
第二个代码(只是删除函数并直接从数组中获取值):
for ($i = 1; $i < 2500; ++$i) {
$pen[$i] = $i * (3 * $i - 1 ) / 2;
}
// function pentagonal($num) {
// global $pen;
// return $pen[$num];
// }
function is_pentagonal($c) {
$x = (1+sqrt(1+24*$c))/(6);
if ($x == (int)$x) {
return true;
} else {
return false;
}
}
for ($i = 2; ; ++$i) {
for ($j = 1; $j < $i ; ++$j) {
$pi = $pen[$i]; // Here is the difference
$pj = $pen[$j]; // Here is the difference
if (is_pentagonal($pi + $pj, $pen)) {
if (is_pentagonal(abs($pi - $pj), $pen)) {
$difference = $pi - $pj;
break 2;
}
}
}
}
echo $i.' '.$j.' '.$difference."\n";
在动态语言中查找全局变量或其他变量通常实现为 table 查找(散列查找或偏移量)。这是非常快的。函数调用总是 "expensive"... 需要执行一定数量的设置和拆卸代码,这可以转换为数千条机器代码指令。相比之下,这很慢。
尽管如此,在更大的系统中用直接变量访问替换所有函数调用是愚蠢的。如果您的问题完全在上面的代码中表达,那么是的,直接访问变量,并在完成后丢弃代码。
如果这是更大系统的一部分,请使用函数。它使测试、调试、静态分析、分析、一切……变得容易得多。即使代码的第一个变体比第二个变体快两倍,它也几乎会淹没在正在进行的所有其他事情的噪音中,尤其是在发生任何 IO 时。
更新:您可以将函数表示为...
function is_pentagonal($c) {
$x = (1+sqrt(1+24*$c))/(6);
return $x == (int)$x;
}
... 因为这将避免创建词法作用域(或在 PHP 中调用的任何内容)。