Optimizing php_html_entities()
| From: | Yasuo Ohgaki | Date: | Tue, 23 Jun 2015 11:37:04 +0000 |
| Subject: | Optimizing php_html_entities() | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-86808@lists.php.net to get a copy of this message | ||
Hi all,
I'm trying to optimize php_html_entities().
Since htmlspecialchars()/htmlentities() are sensitive function, I would
like to ask comments before merge.
https://github.com/php/php-src/pull/1356
Current php_html_entities() convert int/float/etc to string, then convert
it.
int/float/etc is not required to be escaped. Optimize it by simply
converting them to string.
Simple benchmark shows about 60% execution time reduction.
[yohgaki@dev github-php-src]$ ./php-bin b.php
Time: 10.484607934952
[yohgaki@dev github-php-src]$ ./php-bin b.php
Time: 10.867615222931
[yohgaki@dev github-php-src]$ ./php.new b.php
Time: 3.9379420280457
[yohgaki@dev github-php-src]$ ./php.new b.php
Time: 4.0694711208344
[yohgaki@dev github-php-src]$ cat b.php
<?php
const LOOP=100000000;
$start = microtime(true);
for ($i = 0; $i < LOOP; $i++) {
}
$loop_time = microtime(true) - $start;
$start = microtime(true);
for ($i = 0; $i < LOOP; $i++) {
htmlspecialchars(123456790);
}
echo 'Time: '.(microtime(true) - $start - $loop_time)."\n";
--
Yasuo Ohgaki
yohgaki@ohgaki.net