Re: [RFC] [VOTE] Constant scalar expressions
| From: | Tjerk Meesters | Date: | Fri, 22 Nov 2013 05:38:49 +0000 |
| Subject: | Re: [RFC] [VOTE] Constant scalar expressions | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-70277@lists.php.net to get a copy of this message | ||
On Fri, Nov 22, 2013 at 1:11 PM, Yasuo Ohgaki <yohgaki@ohgaki.net> wrote:
> Hi Tjerk,
>
> On Fri, Nov 22, 2013 at 1:34 PM, Tjerk Meesters <tjerk.meesters@gmail.com>wrote:
>
>> Yes, of course that's all possible, because define() is just a function
>> and therefore the normal assignment rules apply. However, you didn't
>> mention the biggest drawback to such an approach, which is that it creates
>> *global* constants; there's no way to namespace them.
>>
>
> What was the reason why we don't have name space for define()?
>
Sorry for the confusion; of course it's possible to supply a fully
qualified name to define(), thus making the constant appear in a namespace.
With namespacing I also meant the ability to "attach" constants to class
definitions, which isn't possible with define().
>
>>> I prefer 'const' to be a static for better performance, since
>>> we have dynamic constant by define() already.
>>>
>>
>> Perhaps we'd have to consider how much performance is impacted on
>> existing code, e.g.
>>
>> class A
>> {
>> const B = 123;
>> }
>>
>> How much slower would that become if this RFC is accepted, cached or
>> otherwise?
>>
>
> I made a script to experiment. I have background job running on this
> machine,
> so I don't get stable result now, but embedding value is constantly faster.
>
> [yohgaki@dev tmp]$ php bench.php
> Time: 6.4675929546356
> Time: 7.4527719020844 15.232544075653% slower
>
That's the time for a define()'ed constant.
> Time: 7.4178550243378 14.69266968975% slower
>
That's the time for a const'ed constant. Still seems faster.
> Time: 8.1120491027832 25.426092205894% slower
>
This is probably due to zend_fetch_class I assume?
> Time: 7.4948840141296 15.883669035753% slower
>
I can't really explain that :)
> [yohgaki@dev tmp]$ php bench.php
> Time: 6.4104981422424
> Time: 7.7053151130676 20.198383060014% slower
> Time: 8.022579908371 25.14752723358% slower
> Time: 7.5032601356506 17.046444272519% slower
> Time: 7.713919878006 20.332612331241% slower
>
This is way too much variation to be reliable.
>
> [yohgaki@dev tmp]$ cat bench.php
> <?php
> define('a', 1);
> const b = 1;
>
> class foo {
> const c = 1;
> }
>
> class bar extends foo {};
>
> $start = microtime(true);
> for ($i=0 ; $i < 99999999; $i++) {
> $v = 1 + 1;
> }
> $test0 = microtime(true) - $start;
> echo 'Time: '. $test0 . PHP_EOL;
>
> $start = microtime(true);
> for ($i=0 ; $i < 99999999; $i++) {
> $v = 1 + a;
> }
> $test = microtime(true) - $start;
> echo 'Time: '. $test . ' '. (($test - $test0)/$test0)*100 . '%
> slower' .
> PHP_EOL;
>
> $start = microtime(true);
> for ($i=0 ; $i < 99999999; $i++) {
> $v = 1 + b;
> }
> $test = microtime(true) - $start;
> echo 'Time: '. $test . ' '. (($test - $test0)/$test0)*100 . '%
> slower' .
> PHP_EOL;
>
> $start = microtime(true);
> for ($i=0 ; $i < 99999999; $i++) {
> $v = 1 + foo::c;
> }
> $test = microtime(true) - $start;
> echo 'Time: '. $test . ' '. (($test - $test0)/$test0)*100 . '%
> slower' .
> PHP_EOL;
>
> $start = microtime(true);
> for ($i=0 ; $i < 99999999; $i++) {
> $v = 1 + bar::c;
> }
> $test = microtime(true) - $start;
> echo 'Time: '. $test . ' '. (($test - $test0)/$test0)*100 . '%
> slower' .
> PHP_EOL;
> ?>
>
Actually, the test should be done against two separate builds:
1) build without patch
2) build with patch
:)
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net
>
--
--
Tjerk