its been a while since i posted here, buy would like a fresh perspective on the problem im facing.
Now as many of us know PHP Array implementation if not as efficient as other languages which is why im thinking i may need to go a different route for this particular problem of my PHP App.
I have the following dataset
[text]Goal: To find the lowest value in the array[/text]
Code: Select all
// this is the sample value of the serialized array for each date (which the initial foreach iterates over
// these elements are also wrapped in an date array eg
// array (
// [2012-06-05] => array // array below
// [2012-06-04] => array // and again etc
Array
(
[80s Casuals] => Array
(
[p] => 10
[u] =>Johnny
)
[Alife] => Array
(
[p] => 48
[u] => Adam
)
[Alife Clothing] => Array
(
[p] => 5
[u] =>Aaron
)
[Alife Sweaters] => Array
(
[p] => 8
[u] => Jamie
)
[Alife T Shirts] => Array
(
[p] => 6
[u] => Alex
)
// etc etc
I loop over each of the 127 array elements checking for the lowest ['p'] valuee and then assign it to the best position, this is the only way i can see, in my mind, i can use to evaluate the best position. This is a design oversight on my part from the projects inception as i hold the values in a serialized array so its incredibly limiting as to what i can do with this in terms of pure SQL queries
Code: Select all
// my current parsing code
// built on codeigniter but this is irrelevant
$start = microtime (true);
$member_id = 12;
$keyword = 'Lacoste';
$best_position = 101;
$this->db->order_by('date', 'asc');
$query = $this->db->get_where('rankings', array('member_id' => $member_id));
if ($query->num_rows() > 0) {
$s = microtime (true);
foreach ($query->result() as $row) {
$rankings[$row->date] = $row->rankings;
}
foreach ($rankings as $date => $serialized) {
$rank_array = unserialize($serialized);
if (isset($rank_array[$keyword]['p']) && $rank_array[$keyword]['p'] != '0') {
// if looped position is less than best position
if ($rank_array[$keyword]['p'] <= $best_position) {
$array = array(
'date' => $date,
'position' => $rank_array[$keyword]['p']
);
$best_position = $rank_array[$keyword]['p'];
}
// break;
}
unset($rank_array);
}
if (isset($array)) {
printr ($array);
echo 'Took '.round((microtime(true) - $start), 5).'s<br />';
echo 'Expected '.round(((microtime(true) - $start) * 137), 5).'s';
exit;
} else {
echo 'array not set';
exit;
}
} else {
echo 'no rankings';
exit;
}[text]
For entire date > element loop: 27.1s
Per 127 inner elements: 0.19s
[/text]
at the moment i do this in real time and obvfiously performance on this particular dataset produces a hige delay in loading the page, i need this to be much much faster,0.2s seems awfully slow for some simple comparisons, theres the min / max functions that i may be able to do something with but i dont think they will help increase performance dramatically here, especially not in the long term, i think PHP array implementation make this not a very good language for operating on the way ive stored the data.
the way i see it ive got a few options
[*]Rewrite this function using some unknown super optimised standard PHP code
[*]Rewrite this as a PHP module which, apart from having to learn some simplish c#(?) would be incredibly fast
[*]Write a seperate routine (scheduled by cron) that gets me this data and stores it as an INT in the database for easy and fast polling
As mentioned i believe its mainly caused by the fact i serialize the data so i cant do any fancy SQL queries to stop my having to loop, this is a design mistake in hindsight but something im stuck with for the time being as changing it is a very big task.
Im basically just looking for some thoughts from fellow developers on what they have / would do in situations like this.
Kind regards, Mal