3v4l.org

run code in 300+ PHP versions simultaneously
<?php function get_content() { return ' person_ID,name,first,last,middle,email,phone,fax,title 3130,"Burks, Rosella ",Rosella,Burks,,BurksR@univ.edu,963.555.1253,963.777.4065,Professor 3297,"Avila, Damien ",Damien,Avila,,AvilaD@univ.edu,963.555.1352,963.777.7914,Professor 3547,"Olsen, Robin ",Robin,Olsen,,OlsenR@univ.edu,963.555.1378,963.777.9262,Assistant Professor 1538,"Moises, Edgar Estes",Edgar,Moises,Estes,MoisesE@univ.edu,963.555.2731x3565,963.777.8264,Professor 2941,"Brian, Heath Pruitt",Heath,Brian,Pruitt,BrianH@univ.edu,963.555.2800,963.777.7249,Associate Curator 2401,"Claude, Elvin Haney",Elvin,Claude,Haney,ClaudeE@univ.edu,963.555.2902,963.777.9730,Curator 2070,"Mosley, Edmund ",Edmund,Mosley,,MosleyE@univ.edu,963.555.2945,963.777.9285,Assistant Professor 2561,"Derek, Antoine Mccoy",Antoine,Derek,Mccoy,DerekA@univ.edu,963.555.2992,963.777.5454,Curator 1625,"Hawkins, Callie ",Callie,Hawkins,,HawkinsC@univ.edu,963.555.3350x6480,963.777.4949,Professor 1307,"Pate, Andrea ",Andrea,Pate,,PateA@univ.edu,963.555.3723,963.777.5049,Professor 2342,"Austin, Liz ",Liz,Austin,,AustinL@univ.edu,963.555.4305,963.777.6143,Assistant Professor 2755,"Kendrick, Reba Alford",Reba,Kendrick,Alford,KendrickR@univ.edu,963.555.4618x7744,963.777.4371,Associate Curator 4150,"Sims, Angelina ",Angelina,Sims,,SimsA@univ.edu,963.555.5278,963.777.4400,Professor 3544,"Mullins, Kimberly ",Kimberly,Mullins,,MullinsK@univ.edu,963.555.5471x1017,963.777.9783,Assistant Professor 2096,"Chuck, Lloyd Haney",Lloyd,Chuck,Haney,ChuckL@univ.edu,963.555.5568x2652,963.777.9290,Assistant Professor 1089,"Payne, Ladonna ",Ladonna,Payne,,PayneL@univ.edu,963.555.5639,963.777.6469,Professor 2948,"Baxter, Johnathan Browning",Johnathan,Baxter,Browning,BaxterJ@univ.edu,963.555.5902,963.777.8336,Research Professor 4539,"Weiss, Gilbert ",Gilbert,Weiss,,WeissG@univ.edu,963.555.5969,963.777.4975,Professor 2811,"Deirdre, Florence Barrera",Florence,Deirdre,Barrera,DeirdreF@univ.edu,963.555.6319,963.777.9654,Associate Curator 4580,"Fernando, Toby Calderon",Toby,Fernando,Calderon,FernandoT@univ.edu,963.555.6469,963.777.9864,Research Professor 2895,"Garrison, Patrica ",Patrica,Garrison,,GarrisonP@univ.edu,963.555.6760,963.777.4958,Associate Curator 2254,"Effie, Leila Vinson",Leila,Effie,Vinson,EffieL@univ.edu,963.555.6824,963.777.7299,Assistant Professor 2389,"Buckley, Rose ",Rose,Buckley,,BuckleyR@univ.edu,963.555.6855x5018,963.777.5233,Curator 1699,"Stanton, Kathie ",Kathie,Stanton,,StantonK@univ.edu,963.555.7095,963.777.1015,Professor 1567,"Banks, Shannon ",Shannon,Banks,,BanksS@univ.edu,963.555.7198,963.777.6979,Professor 3066,"Barnes, Cleo ",Cleo,Barnes,,BarnesC@univ.edu,963.555.7463x7335,963.777.1583,Research Professor 2426,"Brady, Nellie ",Nellie,Brady,,BradyN@univ.edu,963.555.7569,963.777.7218,Curator 2217,"Katheryn, Ruben Holt",Ruben,Katheryn,Holt,KatherynR@univ.edu,963.555.7578,963.777.3969,Assistant Professor 1968,"Michael, Dianne ",Dianne,Michael,,MichaelD@univ.edu,963.555.7592,963.777.9960,Assistant Professor 3012,"Grant, Adam ",Adam,Grant,,GrantA@univ.edu,963.555.7775,963.777.8115,Research Professor 1824,"Head, Kurtis ",Kurtis,Head,,HeadK@univ.edu,963.555.7882,963.777.6348,Professor 3929,"Berger, Jami ",Jami,Berger,,BergerJ@univ.edu,963.555.8158,963.777.5650,Research Professor 2682,"Earline, Jaime Fitzgerald",Jaime,Earline,Fitzgerald,EarlineJ@univ.edu,963.555.8357,963.777.4114,Associate Curator 3112,"Evelyn, Summer Frost",Summer,Evelyn,Frost,EvelynS@univ.edu,963.555.8895,963.777.5730,Professor 2303,"Quentin, Sam Hyde",Sam,Quentin,Hyde,QuentinS@univ.edu,963.555.8921,963.777.2712,Assistant Professor 3903,"Dunlap, Ann ",Ann,Dunlap,,DunlapA@univ.edu,963.555.9067,963.777.4290,Assistant Professor 3095,"Shields, Rich Pena",Rich,Shields,Pena,ShieldsR@univ.edu,963.555.9197,963.777.7215,Professor 2383,"Page, Winnie ",Winnie,Page,,PageW@univ.edu,963.555.9366,963.777.3202,Curator 2146,"Sparks, Ezra ",Ezra,Sparks,,SparksE@univ.edu,963.555.9390,963.777.9273,Assistant Professor '; } $capture = array( 1 => [], 2 => [], ); function captures_differ():bool { global $capture; $diff = false; $n = min(count($capture[1]),count($capture[2])); for($i = 0; $i < $n; $i++) { if ($capture[1][$i] === $capture[2][$i]) { continue; } echo "======>\n"; echo $i, ": '",$capture[1][$i], "' !== '", $capture[2][$i], "'\n"; echo "<======\n"; echo "\n"; $diff = true; } return $diff; } // Function to benchmark strtok function benchmarkStrtok($content, $iterations) { // global $capture; $start = microtime(true); for ($i = 0; $i < $iterations; $i++) { $token = strtok($content, ','); while ($token !== false) { // $capture[1][] = $token; $token = strtok(','); } } $end = microtime(true); return $end - $start; } class StringTokenizer { private \Generator $tokenGenerator; public function __construct(public readonly string $string) { } public function nextToken(string $characters): string|null { if (!isset($this->tokenGenerator)) { $this->tokenGenerator = $this->generator($characters); return $this->tokenGenerator->current(); } return $this->tokenGenerator->send($characters); } private function generator(string $characters): \Generator { $pos = 0; while (true) { $pos += \strspn($this->string, $characters, $pos); $len = \strcspn($this->string, $characters, $pos); if (!$len) return; $token = \substr($this->string, $pos, $len); $characters = yield $token; $pos += $len; } } } function strtok2(string $string, ?string $token = null): string|false { static $tokenGenerator = null; if ($token) { $tokenGenerator = (function(string $characters) use ($string): \Generator { $pos = 0; while (true) { $pos += \strspn($string, $characters, $pos); $len = \strcspn($string, $characters, $pos); if ($len === 0) return; $token = \substr($string, $pos, $len); $characters = yield $token; $pos += $len; } })($token); return $tokenGenerator->current() ?? false; } return $tokenGenerator?->send($string) ?? false; } // Function to benchmark explode function benchmarkStrtok2($content, $iterations) { // global $capture; $start = microtime(true); for ($i = 0; $i < $iterations; $i++) { $token = strtok2($content, ','); while ($token !== false) { // $capture[2][] = $token; $token = strtok2(','); } } $end = microtime(true); return $end - $start; } // Main script execution $content = get_content(); $bytes = strlen($content); $commas = count(explode(",",$content))-1; $iterations = 100; $timeStrtok = benchmarkStrtok($content, $iterations); $timeStrtok2 = benchmarkStrtok2($content, $iterations); //if ( captures_differ() ) { // echo "ERROR: Mismatch"; // exit(1); //} $timesFaster = round( $timeStrtok2 / $timeStrtok,4); $timeStrtok = round($timeStrtok,4); $timeStrtok2 = round($timeStrtok2,4); echo "Size of file: $bytes\n"; echo "Number of commas: $commas\n"; echo "Time for strtok: $timeStrtok seconds\n"; echo "Time for strtok2: $timeStrtok2 seconds\n"; echo "strtok() time delta: $timesFaster\n";
Output for 8.3.9
Size of file: 3956 Number of commas: 359 Time for strtok: 0.0023 seconds Time for strtok2: 0.0109 seconds strtok() time delta: 4.7179

preferences:
152.09 ms | 1410 KiB | 8 Q