All pastes #910778 Raw Edit

Unnamed

public text v1 · immutable
·published 2008-02-20 05:12 UTC
rendered paste body
I have the following script which is preparing user data for output:

// this first line simply read the data from the file into an array.

    if ($val = baw_data($BAW_CURR['site_name'], "VISITOR", $BAW_CURR['yearmonth'])) {
        // I want to summarize people which have a hit/page ratio <1 into the user 'script'
        // so I prepare the array for it.
        $val['script'] = array('script',0,0,0,0,'');
        // now I iterate the data.
        foreach ($val as $dns => $data) {
            // this next line gives me a debug-output and tells me the memory usage & time spent.
            // memory usage at this point: 13.5830 MB
            baw_debug($dns . "start", false, true);
            $ratio = 0;
            // get ratio, prevent div/zero
            if ($data[0] > 0) {
                $ratio = baw_num_format($data[1] / $data[0], 2);
            }
            if ($ratio <= 1) {
                // I want to get the max value of $data[3] since its a timestamp
                $latest_date_bots = max($data[3], $latest_date_bots);
                // now I sum up the data for the 'script'
                $val['script'] = array(
                    $BAW_MES['assumedscript'],
                    $val['script'][0] + $data[0],
                    $val['script'][1] + $data[1],
                    $val['script'][2] + $data[2],
                    baw_num_format(($val['script'][0] + $data[0]) / ($val['script'][1] + $data[1]),2),
                    $latest_date_bots
                );
                // if the user was a script, I dont need the data anymore
                unset($val[$dns]);
            } else {
                otherwise simply rearrange the data for the display function
                $val[] = array($dns,$data[0],$data[1],$data[2],$ratio,$data[3]);
            }
            // check the memory consumption again.
            // after the first round, the memory is at 15.2381 MB !! 1.5MB for one loop!!
            baw_debug($dns . "end", false, true);
            // after this check, when we come to the upper one again, the memory is at 
            // 15.2385 MB, so 400 bytes more than right here, but why?? its only a loop?
            // After each loop, there is +- 200 bytes more memory used. Why? I am only shifting data within the same array!
        }
    }


here are the two functions that are called during the data-handling:

function baw_debug($msg_code, $data = false, $always = false) {
    global $BAW_CONF, $BAW_MES;
    $out = '';
    if ($BAW_CONF['debug'] || $always) {
        if (isset($BAW_MES[ $msg_code])) {
            if ($data) {
                $out .= sprintf($BAW_MES[$msg_code], $data);
            } else {
                $out = $BAW_MES[$msg_code];
            }
        } else {
            $out = $msg_code;
        }
        $memory = baw_byte_format(memory_get_usage(),4);
        $time = baw_ptime();
        echo "<div style=\"text-align:right\">$out | Memory used: $memory | Time: $time</div>\n";
    }
}

function baw_num_format($number, $decimals = 0) {
    global $BAW_CONF;
    if ($number == 0 || empty($number) || !isset($number)) {
        $result = '0';
    } else {
        $result = number_format($number, $decimals, $BAW_CONF['dec_point'], $BAW_CONF['tho_point']);
    }
    return $result;
}