Hi all, On Wed, Jun 24, 2015 at 12:20 PM, Yasuo Ohgaki <yohg...@ohgaki.net> wrote:
> On Wed, Jun 24, 2015 at 12:05 PM, Juan Basso <jrba...@gmail.com> wrote: > >> Did you test the performance impact on strings? Since you changed how it >> works the impact can be positive and maybe worth to make the method more >> broad. > > > It's a bit slower (~3%) for 'abc' as input string. If I remove redundant > type > check(convert_to_string), it's better though. ~3% seems too large for 2 > additional if statements - parameter type check. I'll look into. > > [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('abc'); > } > echo 'Time: '.(microtime(true) - $start - $loop_time)."\n"; > > [yohgaki@dev github-php-src]$ ./php.old b.php > Time: 4.4925589561462 > [yohgaki@dev github-php-src]$ ./php.old b.php > Time: 4.4821939468384 > [yohgaki@dev github-php-src]$ ./php.new b.php > Time: 4.5892491340637 > [yohgaki@dev github-php-src]$ ./php.new b.php > Time: 4.6097931861877 > Followings are better approximation. New code is a bit slower even if it's in benchmark error range. ~1% - ~3% slower for strings while it's much faster (~70% or more. This case is more than 100%) for integers depending on integer size. Since integer/float is much faster, only a few presence of int/float negates negative impact. As you can see, loop only benchmark varies a lot. I also counted total number of CPU instructions by perf. It varies too. However, new code is a bit slower for sure even if there is only 2 "if" addition for strings. I was expecting use of raw zval negates this... I would like to apply similar change to most string escape/conversion functions. (Please note that subject is "escape and conversion" function only) Current patch allows "resource" type, but it should raise type error. Comments are appreciated. Sorry for flood of mails. Regards, [yohgaki@dev github-php-src]$ ./php.old b.php Loop time Loop: 0.08224892616272 Loop: 0.08589506149292 Loop: 0.087536096572876 Loop: 0.082170963287354 Loop: 0.082328081130981 Loop: 0.081686019897461 Loop: 0.087927103042603 Loop: 0.081233024597168 Loop: 0.082290887832642 Loop: 0.081492900848389 Loop: 0.081452131271362 Loop: 0.081291913986206 Loop Avg: 0.08220899105072 Escaping string abcdefgh Time: 0.6288548707962 Time: 0.61239206790924 Time: 0.6169821023941 Time: 0.61680710315704 Time: 0.60974395275116 Time: 0.61229383945465 Time: 0.61705410480499 Time: 0.61985099315643 Time: 0.61954915523529 Time: 0.62286603450775 Time: 0.62001097202301 Time: 0.6216961145401 Time Avg: 0.6166380405426 Escaping integer 12345678 Time: 0.98103010654449 Time: 0.97324693202972 Time: 0.96535289287567 Time: 0.962033867836 Time: 0.95926511287689 Time: 0.9667671918869 Time: 0.96851003170013 Time: 0.96103513240814 Time: 0.96122300624847 Time: 0.95892608165741 Time: 0.96553599834442 Time: 0.96350586414337 Time Avg: 0.96321551799774 [yohgaki@dev github-php-src]$ ./php.new b.php Loop time Loop: 0.082480907440186 Loop: 0.081701993942261 Loop: 0.081080913543701 Loop: 0.080965042114258 Loop: 0.081230163574219 Loop: 0.08064603805542 Loop: 0.081595897674561 Loop: 0.081001043319702 Loop: 0.080890893936157 Loop: 0.084527969360352 Loop: 0.082308053970337 Loop: 0.081248998641968 Loop Avg: 0.081266903877258 Escaping string abcdefgh Time: 0.62283012866974 Time: 0.62788126468658 Time: 0.63206312656403 Time: 0.62420103549957 Time: 0.62370917797089 Time: 0.62745521068573 Time: 0.626731133461 Time: 0.68132016658783 Time: 0.62430212497711 Time: 0.63127825260162 Time: 0.63222811222076 Time: 0.62956402301788 Time Avg: 0.62700154781342 Escaping integer 12345678 Time: 0.41526029109955 Time: 0.41403410434723 Time: 0.41262814998627 Time: 0.41230318546295 Time: 0.41555306911469 Time: 0.40698001384735 Time: 0.41639611721039 Time: 0.41320106983185 Time: 0.41369721889496 Time: 0.42157099246979 Time: 0.42368505001068 Time: 0.42184422016144 Time Avg: 0.4141624212265 [yohgaki@dev github-php-src]$ cat b.php <?php const LOOP=10000000; const ITER=12; echo "Loop time\n"; $t = []; for ($n = 0; $n < ITER; $n++) { $start = microtime(true); for ($i = 0; $i < LOOP; $i++) { } $loop_time = microtime(true) - $start; echo 'Loop: '.$loop_time."\n"; $t[] = $loop_time; } // Remove max for better approximation unset($t[array_search(max($t), $t)]); unset($t[array_search(max($t), $t)]); $loop_time = array_sum($t)/(ITER-2); echo "Loop Avg: ${loop_time}\n"; $s = 'abcdefgh'; echo "Escaping string ${s}\n"; $t = []; for ($n = 0; $n < ITER; $n++) { $start = microtime(true); for ($i = 0; $i < LOOP; $i++) { htmlspecialchars($s); } $time = (microtime(true) - $start - $loop_time); echo "Time: ${time}\n"; $t[] = $time; } // Remove max for better approximation unset($t[array_search(max($t), $t)]); unset($t[array_search(max($t), $t)]); $time = array_sum($t)/(ITER-2); echo "Time Avg: ${time}\n"; $s = 12345678; echo "Escaping integer ${s}\n"; $t = []; for ($n = 0; $n < ITER; $n++) { $start = microtime(true); for ($i = 0; $i < LOOP; $i++) { htmlspecialchars($s); } $time = (microtime(true) - $start - $loop_time); echo "Time: ${time}\n"; $t[] = $time; } // Remove max for better approximation unset($t[array_search(max($t), $t)]); unset($t[array_search(max($t), $t)]); $time = array_sum($t)/(ITER-2); echo "Time Avg: ${time}\n"; -- Yasuo Ohgaki yohg...@ohgaki.net