Req #79545 [NEW]: mbstring functions are about 10x slower

From: Date: Thu, 30 Apr 2020 11:00:56 +0000
Subject: Req #79545 [NEW]: mbstring functions are about 10x slower
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226850@lists.php.net to get a copy of this message
From:             michael dot vorisek at emailc dot z
Operating system: Linux
PHP version:      7.4.5
Package:          Performance problem
Bug Type:         Feature/Change Request
Bug description:mbstring functions are about 10x slower

Description:
------------
https://3v4l.org/bOdXP

Currently the performance difference between single-byte and multi-byte
string function is about 5x - 25x (yes, +400% - +2400%).

Please verify and comment.

If the performance can not be improved with mbstring, then checking if
the input string does contain only 0x00 - 0x7f chars will improve the
overall/real PHP performace - check can be done very quickly using
modern/vectored CPU instructions - and if satisfied, use equivalent
single-byte string function instead of multi-byte one.

Test script:
---------------
$cnt = 100000;

$strs = [
    'empty' => '',
    'short' => 'zluty kun',
    'short_with_uc' => 'zluty Kun',
    'long' => str_repeat('this is about 10000 chars long string', 270),
    'long_with_uc' => str_repeat('this is about 10000 chars long
String', 270),
    'short_utf8' => 'žlutý kůň',
    'short_utf8_with_uc' => 'Žlutý kŮň',
];

foreach ($strs as $k => $str) {
    $a1 = microtime(true);
    for($i=0; $i < $cnt; ++$i){
        $res = strtolower($str);
    }
    $t1 = microtime(true) - $a1;
    // echo 'it took ' . round($t1 * 1000, 3) . ' ms for ++$i'."\n";

    $a2 = microtime(true);
    for($i=0; $i < $cnt; $i++){
        $res = mb_strtolower($str);
    }
    $t2 = microtime(true) - $a2;
    // echo 'it took ' . round($t2 * 1000, 3) . ' ms for $i++'."\n";

    echo 'strtolower is '.round($t2/$t1, 2).'x faster than mb_strtolower
for ' . $k . "\n\n";
}


Expected result:
----------------
strtolower is 6.73x faster than mb_strtolower for empty

strtolower is 9.78x faster than mb_strtolower for short

strtolower is 7.13x faster than mb_strtolower for short_with_uc

strtolower is 24.88x faster than mb_strtolower for long

strtolower is 23.12x faster than mb_strtolower for long_with_uc

strtolower is 9.05x faster than mb_strtolower for short_utf8

strtolower is 9.93x faster than mb_strtolower for short_utf8_with_uc



Actual result:
--------------
Difference should be no larger than 1.5x at least for single-byte
strings.

-- 
Edit bug report at https://bugs.php.net/bug.php?id=79545&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=79545&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=79545&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=79545&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=79545&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=79545&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=79545&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=79545&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=79545&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=79545&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=79545&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79545&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=79545&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=79545&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=79545&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=79545&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=79545&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=79545&r=mysqlcfg


Thread (5 messages)

« previous php.bugs (#226850) next »