Re: [RFC] [VOTE] Constant scalar expressions
| From: | Yasuo Ohgaki | Date: | Fri, 22 Nov 2013 05:11:25 +0000 |
| Subject: | Re: [RFC] [VOTE] Constant scalar expressions | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-70276@lists.php.net to get a copy of this message | ||
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()?
>> 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
Time: 7.4178550243378 14.69266968975% slower
Time: 8.1120491027832 25.426092205894% slower
Time: 7.4948840141296 15.883669035753% slower
[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
[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;
?>
--
Yasuo Ohgaki
yohgaki@ohgaki.net