Bug #15834
closedt3lib_div::trimExplode() is heavily called and can be optimized => nearly 1% improvement in fronEnd (both cached and uncached)
0%
Description
/!\ PHP >= 4.0.6
Using xDebug Profiler, I have noticed nearly 1% improvement in fronEnd (both cached or uncached) !
It seems like a 1% "almost free" optimization...
function trimExplode($delim, $string, $onlyNonEmptyValues=0) {
if ($onlyNonEmptyValues) {
return array_diff(array_map('trim', explode($delim, $string)), array(''));
} else {
return array_map('trim', explode($delim, $string));
}
}
"Unit" TEST :
function trimExplodeNEW($delim, $string, $onlyNonEmptyValues=0) {
if ($onlyNonEmptyValues) {
return array_diff(array_map('trim', explode($delim, $string)), array(''));
} else {
return array_map('trim', explode($delim, $string));
}
}
function trimExplodeOLD($delim, $string, $onlyNonEmptyValues=0) {
$temp = explode($delim,$string);
$newtemp=array();
while(list($key,$val)=each($temp)) {
if (!$onlyNonEmptyValues || strcmp('',trim($val))) {
$newtemp[]=trim($val);
}
}
reset($newtemp);
return $newtemp;
}
function array_strict_compare($res1, $res2) {
$res1 = implode(',', $res1);
$res2 = implode(',', $res2);
if ($res1 !== $res2) return false;
return true;
}
$tests = array(
', a, 1 ,c ,dddd , ,',
", 0, , \n , , aaa , 00,",
'1,2,3,4,5.6,7,8,9,10,11',
implode(',', array_fill(0, 1000, 'test')),
implode(' , ', array_fill(0, 100, 'test')),
' ,, ,,'.implode(' , ', array_fill(0, 100, 'test')).',, ,, ',
' ,, ,,'.implode(' , , ', array_fill(0, 100, 'test')).',, ,, ',
implode(',', range(0, 1000)),
implode(' , ', range(0, 1000)),
);
print "
\n"; foreach ($tests as $test) { foreach (array(true, false) as $onlyNonEmptyValues) { $res1 = trimExplodeOLD(',', $test, $onlyNonEmptyValues); $res2 = trimExplodeNEW(',', $test, $onlyNonEmptyValues); if (! array_strict_compare($res1, $res2)) { print '$test = '.$test."\n"; print '$res1 = '.implode(',', $res1)."\n"; print '$res2 = '.implode(',', $res2)."\n"; print "===> KO\n\n"; } else { //print '$test = '.$test."\n"; //print '$res1 = '.implode(',', $res1)."\n"; //print '$res2 = '.implode(',', $res2)."\n"; print "===> OK\n\n"; } } } print "\n";
?>
"Live TEST" (in class.t3lib_div.php):
/**
* Explodes a string and trims all values for whitespace in the ends.
* If $onlyNonEmptyValues is set, then all blank ('') values are removed.
* Usage: 256
*
* @param string Delimiter string to explode with
* @param string The string to explode
* @param boolean If set, all empty values (='') will NOT be set in output
* @return array Exploded values
*/
function trimExplode($delim, $string, $onlyNonEmptyValues=0) {
$res1 = t3lib_div::trimExplodeOLD($delim, $string, $onlyNonEmptyValues);
$res2 = t3lib_div::trimExplodeNEW($delim, $string, $onlyNonEmptyValues);
$res1t = implode(',', $res1);
$res2t = implode(',', $res2);
if ($res1t !== $res2t) {
t3lib_div::debug($res1, 'trimExplodeNEW() error $res1');
t3lib_div::debug($res2, 'trimExplodeNEW() error $res2');
return $res1;
}
return $res2;
}
function trimExplodeNEW($delim, $string, $onlyNonEmptyValues=0) {
if ($onlyNonEmptyValues) {
return array_diff(array_map('trim', explode($delim, $string)), array(''));
} else {
return array_map('trim', explode($delim, $string));
}
}
function trimExplodeOLD($delim, $string, $onlyNonEmptyValues=0) {
$temp = explode($delim,$string);
$newtemp=array();
while(list($key,$val)=each($temp)) {
if (!$onlyNonEmptyValues || strcmp('',trim($val))) {
$newtemp[]=trim($val);
}
}
reset($newtemp);
return $newtemp;
}
(issue imported from #M2883)