Bug #74933 [Opn->Nab]: MBstring functions are much slower when called with encoding parameter

From: Date: Tue, 18 Jul 2017 07:53:33 +0000
Subject: Bug #74933 [Opn->Nab]: MBstring functions are much slower when called with encoding parameter
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-210100@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74933&edit=1 ID: 74933 Updated by: jhdxr@php.net Reported by: reinir dot puradinata at gmail dot com Summary: MBstring functions are much slower when called with encoding parameter -Status: Open +Status: Not a bug Type: Bug Package: mbstring related Operating System: Windows PHP Version: 7.1.7 Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Whenever you passed in a string, php has to converted it into internal enum type, and it's why the second case is so slow in your test file. Previous Comments: ------------------------------------------------------------------------ [2017-07-17 01:08:57] reinir dot puradinata at gmail dot com Description: ------------ Several MBstring functions are much slower when called with encoding parameter than without encoding parameter. Functions that exhibit this behavior are mb_strlen, mb_substr, mb_strpos, mb_strrpos. For more information see: https://stackoverflow.com/questions/45028018/a/45107408 Test script: --------------- mb_internal_encoding("UTF-8"); echo "without encoding parameter:\n"; $a = microtime(true); for($i=0; $i<100000; $i++){ $n = mb_strlen("あえいおう"); } $a = microtime(true)-$a; echo "- mb_strlen: ".number_format($a*1000)." ms\n"; echo "\nwith encoding parameter:\n"; $b = microtime(true); for($i=0; $i<100000; $i++){ $n = mb_strlen("あえいおう", "UTF-8"); } $b = microtime(true)-$b; echo "- mb_strlen: ".number_format($b*1000)." ms (".number_format(($b-$a)*100/$a)."% slower)\n"; Expected result: ---------------- Because the character encoding is UTF-8 in both cases, I expected to see similar performance. In other words, 0% slower or close to it. Actual result: -------------- When called with encoding parameter, performance drops greatly. Example output from the test script: without encoding parameter: - mb_strlen: 14 ms with encoding parameter: - mb_strlen: 585 ms (4,186% slower) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=74933&edit=1

« previous php.bugs (#210100) next »