用于优化的静态变量

发布于 2024-08-25 12:21:12 字数 477 浏览 11 评论 0原文

我想知道是否可以使用静态变量进行优化:

public function Bar() {
    static $i = moderatelyExpensiveFunctionCall();
    if ($i) {
        return something();
    } else {
        return somethingElse();
    }
}

我知道一旦 $i 被初始化,它就不会被连续调用 Bar 时的该行代码所改变()。我假设这意味着moderatelyExpectiveFunctionCall()不会在我每次调用时都被评估,但我想确定一下。

一旦 PHP 看到一个已初始化的静态变量,它是否会跳过该行代码?换句话说,如果我对 Bar() 进行大量调用,这会优化我的执行时间,还是在浪费时间?

I'm wondering if I can use a static variable for optimization:

public function Bar() {
    static $i = moderatelyExpensiveFunctionCall();
    if ($i) {
        return something();
    } else {
        return somethingElse();
    }
}

I know that once $i is initialized, it won't be changed by by that line of code on successive calls to Bar(). I assume this means that moderatelyExpensiveFunctionCall() won't be evaluated every time I call, but I'd like to know for certain.

Once PHP sees a static variable that has been initialized, does it skip over that line of code? In other words, is this going to optimize my execution time if I make a lot of calls to Bar(), or am I wasting my time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

超可爱的懒熊 2024-09-01 12:21:12

我发现执行下面的代码更容易。这样缓存是全局完成的,而不是每个函数的实现。

function moderatelyExpensiveFunctionCall()
{
   static $output = NULL;
   if( is_null( $output ) ) {
     //set $output
   }
   return $output;
}

I find it easier to do something like the code below. That way the caching is done globally instead of per implementation of the function.

function moderatelyExpensiveFunctionCall()
{
   static $output = NULL;
   if( is_null( $output ) ) {
     //set $output
   }
   return $output;
}
伪装你 2024-09-01 12:21:12

static $i = blah() 无法编译,因为 php 不允许在静态初始值设定项中进行表达式和函数调用。你需要类似的东西

function foo() {
   static $cache = null;

   if(is_null($cache)) $cache = expensive_func();

   do something with $cache
}

static $i = blah() won't compile, because php doesn't allow expressions and function calls in static initializers. You need something like

function foo() {
   static $cache = null;

   if(is_null($cache)) $cache = expensive_func();

   do something with $cache
}
许一世地老天荒 2024-09-01 12:21:12

这应该适用于您的(非常简单)情况:

function your_function() {
    static $output;

    if (!isset($output)) {
       $output = 'A very expensive operation';
    }

    return $output;
}

对于全局缓存机制,您可以使用类似于 这个

This should work in your (quite simple) case:

function your_function() {
    static $output;

    if (!isset($output)) {
       $output = 'A very expensive operation';
    }

    return $output;
}

As for a global caching mechanism, you may use a method similar to this one.

演多会厌 2024-09-01 12:21:12

这是一个相当简短的方法:

function stuff()
{
    static $smthg = []; // or null, or false, or something else
    if ($smthg) {
        return $smthg;
    }

    // filling $smthg goes here 
    // with a lot of 
    // code strings

    return $smthg;
}

Here is quite shorter approach:

function stuff()
{
    static $smthg = []; // or null, or false, or something else
    if ($smthg) {
        return $smthg;
    }

    // filling $smthg goes here 
    // with a lot of 
    // code strings

    return $smthg;
}
对不⑦ 2024-09-01 12:21:12

怎么样:

if (!isset($i))
{
    static $i = moderatelyExpensiveFunctionCall();
}

How about:

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