Jump to content

Speeding up iterative ARRAY processing


fiat

Recommended Posts

Hi Guys,

i have a multidimensional array counting information from a bike computer.

Heart rate -> speed -> cadence -> altitude -> power -> time

I've created an ugly function that works but take >2minutes to execute..

The function generates an average power output over a time interval of $timecount (in seconds)

The main problem is the time in the array is no uniformly incremental (ie 1,3,4,6,7)

Can anyone suggest a quicker way of processing an array. I've worked out a nicer way using SQL - but it is slower!!

Thanks

$top - holds the rows in the array
$data - holds all the data that has been read in from a sql table.

[code]

<?php


function analysethis($timecount) {

global  $top, $data;
 
$a = 0; //start
$b = $timecount; //end second interval to iterate through
$num = 0; //divisor
$x = 0;
$totalwatts = 0;
$highwatts = 0;

// now need to iterate through each loop where X is less than >=0 and <=5 - then need to iterate through X+1


while ( $x < $top ) {

$minutes = $data[$x][minutes];
$watts = $data[$x][watts];
$cadence = $data[$x][cadence];
$speed = $data[$x][speed];
$hrate = $data[$x][hrate];



if ($minutes >= $a && $minutes <= $b) {


$totalwatts = $watts + $totalwatts;
$num++;

}



if ($minutes > $b) {

if ($num == 0) {$num = 100;}

$totalwatts  = $totalwatts / $num;



if ($totalwatts > $highwatts) {$highwatts = $totalwatts;}



//reset variables and loop again
$totalwatts = 0;
$a++;
$b++;
$x=x-($num);
$num = 0;
}

$x++;
}


echo " max $timecount sec power output -> $highwatts<br>";



flush();

}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/34674-speeding-up-iterative-array-processing/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.