get_defined_vars()对性能的影响是什么? [英] What is the performance impact off `get_defined_vars()`?

查看:155
本文介绍了get_defined_vars()对性能的影响是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常看到关于SO的问题,这些问题的答案/解决方案包括使用get_defined_vars()或使用该方法的Internet上的博客文章.在大多数情况下,它用于调试目的,但在某些情况下,似乎作者有意在生产代码中使用它.

Every so often I see questions on SO whose answers/solutions include the use of get_defined_vars(), or blog posts on the internet that use the method. In most instances it is used for debugging purposes, but in some instances it seems as though the authors have the intention to use it in production code.

虽然我很少使用该函数,因为它使我感到内在,但是我想知道在PHP应用程序中使用此函数的实际性能影响是什么.

While I rarely use the function because it makes me feel off inside, I was wondering what the actual performance impact of using this function was in a PHP application.

一个示例的用法可能是查看变量是否明确设置为NULL:

One example of it's usage may be to see if a variable is explicitly set to NULL:

//$implicit_null = 1234;
$explicit_null = NULL;

var_dump(is_null($implicit_null)); // TRUE, also throws undefined index error
var_dump((
    array_key_exists('implicit_null',get_defined_vars()) && 
    is_null($implicit_null)
)); // FALSE

Internet上还有其他用例,但很少概述性能或内存影响.

There are other use-cases circulating on the internet, but very little outlining what the performance or memory impacts may be.

推荐答案

事实证明,内存影响可能会有所不同,但在最坏的情况下,它几乎可以将您的内存使用量翻倍.

It turns out the memory impact can vary, but in worst-case, it can nearly double your memory usage.

function report_memory($string = '') {
    $mem = (memory_get_usage()/1000);
    echo "$string: {$mem}kb\n";
    return $mem;
}

// ~117.164kb
$start = report_memory('Start of stript');

for($i = 10000; $i > 0; $i--) {
    $var = "filler_$i";
    $$var = 'banana';
}

// ~1022.752kb after fill
$fill_size = ($after_fill = report_memory('After banana')) - $start;
// ~905.588kb fill size
echo "Fill Size: {$fill_size}kb\n\n";

$tmp_vars = get_defined_vars();

// ~1649.12kb after function call
$grew = report_memory('After get_defined_vars()') - $after_fill;
// ~626.368kb growth due to call
echo "Growth from get_defined_vars(): {$grew}kb\n\n"; 

同样,这是最坏的情况.当用array_fill()填充数组时,确实看到了一些奇怪的行为.如您所见,与自己创建变量相比,调用get_defined_vars()的增长非常小.我起初以为这是由于array被返回为引用,但这是显然不是这种情况.但是,还应注意,对象将像往常一样返回为引用.

Again, this is worst-case. I did see some odd behavior when simply filling an array with array_fill(). As you can see here, the growth from calling get_defined_vars() is VERY small, compared to creating variables themselves. I thought, at first, that this was due to arrays being returned as references, but this is clearly not the case. It should also be noted, however, that objects WILL be returned as references, as objects always are.

所有这些,不可能所有的全局变量都将在一个数组接一个数组的情况下出现,并且所有这些字符串,二进制数据和数字都将很快相加.

All of that said, it is unlikely that all of your global variables are going to be arrays upon arrays upon arrays, and all of those strings, binary data, and numbers are going to add up fairly quickly.

最后,这是一个函数,几乎可以使您的内存占用空间增加一倍,并且会产生大量的挂钟时间:

In the end, this is a single function that can nearly double your memory footprint, and incurs a significant wall-time:

for($i = 10000; $i > 0; $i--) {
    $var = "filler_$i";
    $$var = 'banana';
}

$start = microtime(true);

for($i = 250; $i > 0; $i--) {
    $all = get_defined_vars();
}

$stop = microtime(TRUE);
echo round(((($stop - $start))/250)*1000000, 2); // ~1967.45 microseconds

一般结论

仅使用 进行调试,我什至不习惯整夜将其留在代码中.谨慎使用.

Use only for debugging, I wouldn't even get in the habit of leaving it in the code over night. Use sparingly.

这篇关于get_defined_vars()对性能的影响是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆