note 127161 deleted from function.grapheme-strlen by girgias

From: 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"; } ?>

« previous php.notes (#218564) next »