<?php /* * Run pinned to specific CPU core: * taskset -c 3 php perf-regex-printable-characters.php */ $regex = '/[\x{0000}-\x{0020}\x{007F}-\x{009F}"\'>\/=\x{FDD0}-\x{FDEF}' . '\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}' . '\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}' . '\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}' . '\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}' . '\x{10FFFE}\x{10FFFF}]/u'; $subjectInterned = 'weengahhoghecho1eeSheeG5el9cie5b'; // Interned string - is never flagged IS_STR_VALID_UTF8 - and thereby needs repeated UTF-8 validation. $text = 'weengahhoghecho1eeSheeG5el9cie5'; $subjectWithFlagValidUtf8 = $text . 'b'; // Gets flagged IS_STR_VALID_UTF8 after very first check - e.g. triggered by `preg_matach('//u', …)` $tests = ['A', 'A', 'B', 'A', 'B', 'A', 'B']; $testRun = 0; foreach ($tests as $test) { $pattern = $regex; switch ($test) { case 'A': // triggers repeated UTF-8 validation on each run of preg_match $subject = $subjectInterned; break; case 'B': // one-time UTF-8 validation $subject = $subjectWithFlagValidUtf8; break; } $start = microtime( true ); for ($i = 0; $i < 1000000; $i++) { preg_match($pattern, $subject); } $diff = sprintf( '%.6f sec.', microtime( true ) - $start ); if ($testRun > 0) { echo "Execution time $test: $diff\n"; } $testRun++; gc_collect_cycles(); }
You have javascript disabled. You will not be able to edit any code.