[SOLVED] Huge code timing difference: can't understand why
Posted: Thu Mar 10, 2005 10:54 pm
I have two versions of what essentially do the same thing: take an array of values and create an array->key->key = value.
The second one was suggested by someone else to use references:
When this repeats only, say, 50 times, the difference is neglible. But when I use my huge "readall" script, the first executes in 10 - 20 seconds, and the other one times out (with a max exec of 120 sec).
I can't understand this. Why is the second one taking so long?
BTW: in the sample I'm citing, each of these is repeated about a 2000 times.
Code: Select all
$code = "\${$iterate[0]}";
array_shift($iterate);
foreach ($iterate as $readfile_value) {
if ($readfile_value == "") {
$code .= "[]";
} else {
$code .= "['$readfile_value']";
}
}
$code .= " = \$data;";
eval($code);Code: Select all
$code =& ${$iterate[0]};
array_shift($iterate);
for ($i = 0; isset($iterate[$i]); $i++) {
if (!isset($code)) {
$code = array();
}
if (isset($iterate[$i+1])) {
if ($iterate[$i] !== "") {
$code =& $code[$iterate[$i]];
} else {
$code =& $code[];
}
} else {
if ($iterate[$i] !== "") {
$code[$iterate[$i]] = $data;
} else {
$code[] = $data;
}
}
}I can't understand this. Why is the second one taking so long?
BTW: in the sample I'm citing, each of these is repeated about a 2000 times.