note 127161 deleted from function.grapheme-strlen by girgias
| From: | girgias@php.net | Date: | Wed, 08 Jun 2022 14:37:39 +0000 |
| Subject: | note 127161 deleted from function.grapheme-strlen by girgias | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218564@lists.php.net to get a copy of this message | ||
Note Submitter: chokolatrix at gmail dot com
----
Benchmark that compares the speed between "strlen", "mb_strlen",
"grapheme_strlen" on a short unicode string.
TLDR; (tested with PHP 7.4 and 8.0)
========================================
strlen 0.0052 seconds Ã1
mb_strlen 0.2471 seconds Ã50
grapheme_strlen 2.2879 seconds Ã450
========================================
<?php
$cycles = 100_000;
$string = str_repeat('my flag
ð´ó §ó ¢ó ¥ó ®ó §ó ¿', 50) ;
$bench = function ($function) use ($string, $cycles) {
for ($i = 0; $i < $cycles; $i++) {
$function($string);
}
};
foreach (['strlen', 'mb_strlen', 'grapheme_strlen'] as $function)
{
$t = microtime(true);
$bench($function);
echo $function . ' ' . round(microtime(true) - $t, 4) . " seconds \n";
}
?>