PHP custom function code optimization

Posted by Alex on Stack Overflow See other posts from Stack Overflow or by Alex
Published on 2010-04-20T16:13:24Z Indexed on 2010/05/04 3:08 UTC
Read the original article Hit count: 203

Filed under:
|
|

Now comes the hard part. How do you optimize this function:

function coin_matrix($test, $revs) {
    $coin = array();

    for ($i = 0; $i < count($test); $i++) {
        foreach ($revs as $j => $rev) {
            foreach ($revs as $k => $rev) {
            if ($j != $k && 
                $test[$i][$j] != null && 
                $test[$i][$k] != null) {
                    if(!isset($coin[$test[$i][$j]])) {
                        $coin[$test[$i][$j]] = array(); 
                    }
                    if(!isset($coin[$test[$i][$j]][$test[$i][$k]])) {
                        $coin[$test[$i][$j]][$test[$i][$k]] = 0; 
                    }

                    $coin[$test[$i][$j]][$test[$i][$k]] += 1 / ($some_var - 1);
                }
            }
        }
    }
    return $coin;
}

I'm not that good at this and if the arrays are large, it runs forever.

The function is supposed to find all pairs of values from a two-dim array and sum them like this: $coin[$i][$j] += sum_of_pairs_in_array_row / [count(elements_of_row) - 1]

Thanks a lot!

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays