<?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;
}
// Function to benchmark explode
function benchmarkModernStrtok($content, $iterations) {
//global $capture;
$start = microtime(true);
$content_len = strlen($content);
for ($i = 0; $i < $iterations; $i++) {
$pos = 0;
while (true) {
$pos += \strspn($content, ',', $pos);
$len = \strcspn($content, ',', $pos);
$token = substr($content, $pos, $len);
if ($pos >= $content_len ) {
break;
}
$pos += $len;
//$capture[2][] = $token;
}
}
$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);
$timeMS = benchmarkModernStrtok($content, $iterations);
//if ( captures_differ() ) {
// echo "ERROR: Mismatch";
// exit(1);
//}
$timesFaster = round($timeMS/$timeStrtok,4);
$timeStrtok = round($timeStrtok,4);
$timeMS = round($timeMS,4);
echo "Size of file: $bytes\n";
echo "Number of commas: $commas\n";
echo "Time for strtok: $timeStrtok seconds\n";
echo "Time for ModernStrTok: $timeMS 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.0009 seconds
Time for ModernStrTok: 0.0026 seconds
strtok() time delta: 2.8192
preferences:
52.87 ms | 405 KiB | 5 Q