Jump to content

speeding up a function


dingus

Recommended Posts

hey guys i have a inverse chi square function that is running takeing quite a long time to run it currently looks like this

 

function chi2Q( $x,  $v) {
        bcscale(1500);
        $m = bcdiv($x , '2.0');
	$s = bcdiv('1' , bcpow('2.718282', $m));
        $t = $s;
        
        for($i=1; $i < (bcdiv($v,2));$i++) {
            $t = bcmul($t, bcdiv($m,$i));
            $s = bcadd($s,$t);
        }

        return $s ;
    }

 

no i need to use bcmath to stop a underflow problem from with in that function..... and i have no idea how to incrse the speed of this function the for loop is looping around 10000 times

 

now i have tryed using gmp to do it except that only supports really big numbers not really small ones (only works with int as fare as i can work out)

 

can anyone help me speed this up?

Link to comment
https://forums.phpfreaks.com/topic/103120-speeding-up-a-function/
Share on other sites

Evaluate the loop end condition once instead of every loop

 

        $k = bcdiv($v,2);
        for($i=1; $i < $k; $i++) {
            $t = bcmul($t, bcdiv($m,$i));
            $s = bcadd($s,$t);
        }

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.