Which is faster? Constants, Variables or Variable Arrays

前端 未结 6 987
清酒与你
清酒与你 2020-12-03 13:38

My current web application uses about 30 or so Contants (DEFINE()). I am reading things that variables are quicker. Provided that there is a naming convention to avoid varia

相关标签:
6条回答
  • 2020-12-03 14:10

    The difference would be really small (micro optimizations). You would better encapsulate some of your constants in classes so you can access them by Classname::CONSTANT to not pollute the global namespace of your application.

    0 讨论(0)
  • 2020-12-03 14:14

    The results of these benchmark still holds; variables are faster than both define() and const: http://planetozh.com/blog/2006/06/php-variables-vs-constants

    http://www.joomlaperformance.com/articles/performance/52_php_programming_tips_43_13_2.html

    0 讨论(0)
  • 2020-12-03 14:18

    A quick test showed that defining constants (define('FOO', 'bar');) is about 16 to 18 times slower than defining a variable ($foo = 'bar';), but using the defined (constant) value is about 4 to 6 times faster.

    0 讨论(0)
  • 2020-12-03 14:18

    I also can't imagine that any speed differential would be consequential. What is certainly true is that variables are much easier to use than are constants in many cases.

    However it seems your real problem is that you have a bunch of configuration data and you want to avoid having to pass loads of variables to functions, or have to make them all global.

    There is no one answer that works for everyone, but a popular solution is to either utilize a registry class or create one yourself. You can do this pretty easily by declaring a php array. Then you initialize the registry object, and your functions and classes access it through a static method call. Rather than provide you snippets, I'll just refer you to zend_config and zend_registry as examples. While ZF provides these, you should not be afraid to roll your own versions, as neither is particularly hard to recreate in a simpler form if you want to avoid the use of ZF in your project.

    0 讨论(0)
  • 2020-12-03 14:22

    I was benchmarking constants vs variables and noticed a significant improvement of performance when using variables over constants. I know it is pretty obvious but it is definietely worth taking into the cosideration that use of local variables over constants whenever possible.

    If constants are being used inside loops number of times, it is definitely worth declaring the constant as a class / local variable and use it instead.

    The benchmarking test case includes creating two functions. Each has a loop that executes 10000000 times. One access a constant declared in a constant file and one access a local variable.

    TestConstants.php

    class TestConstants 
    {   
        const TEST_CONSTANT = 'This is a constant value';
    
    }
    

    Test.php

    use TestConstants;
    
    class Test {
    
        protected $TEST_CONSTANT;
        protected $limit = 10000000;
        function __construct() {
            $this->TEST_CONSTANT = 'This is a constant value';
        }
    
        function testA() {
            $limit = $this->limit;
            $time_start = microtime(true); 
            for ($i = 0; $i < $limit; ++$i) {
                TestConstants::TEST_CONSTANT;
            }
            $time_end = microtime(true);
            $execution_time = ($time_end - $time_start);
            echo ''. $execution_time .' seconds <br/>';
        }
    
        function testB() {
            $limit = $this->limit;
            $time_start = microtime(true); 
            for ($i = 0; $i < $limit; ++$i) {
                $this->TEST_CONSTANT;
            }
            $time_end = microtime(true);
            $execution_time = ($time_end - $time_start);
            echo ''. $execution_time .' seconds <br/>';
        }   
    }
    
    $test = new Test();
    $test->testA();
    $test->testB();
    

    Results are as follows

    testA() executes in 0.55921387672424 seconds

    and

    testB() executes in 0.33076691627502 seconds

    PHP Version

    5.6.30

    I thought to share this as someone out there might be benefitted by avoiding direct calls to constants (especially inside loops) by declaring them as variables wherever applicable.

    Thank you.

    0 讨论(0)
  • 2020-12-03 14:24

    Constants defined using define() are fairly slow in PHP. People actually wrote extensions (like hidef) to improve the performance.

    But unless you have loads of constants this shouldn't make much of a difference.

    As of PHP 5.3 you can also use compile-time constants using const NAME = VALUE;. Those are much faster.

    0 讨论(0)
提交回复
热议问题