PHP object array's not linearly scale while global arrays do?

前端 未结 2 600
滥情空心
滥情空心 2021-02-08 16:32

There is a major performance issue when using in-object array\'s as a property versus using a global php array variable, why?

To benchmark this problem I created the fol

2条回答
  •  暖寄归人
    2021-02-08 16:43

    I can't post this all in a comment, so this is more of an observation than an answer. It looks like SplObjectStorage is fairly slow. Also that array_push is a lot faster than $array[] = 'item';

    Disclaimer: Apologies for the sloppy code :)

    data[] = $obj;
            }
            break;
    
        case 4:
            class Test {
                public static $data = array();
            }
            $s = new Test;
            for ($i = 0; $i < $iteration; $i++) {
                $obj = new stdClass;
                $s->data[] = $obj;
            }
            break;  
        case 5:
            class Test {
                public $data = array();
            }
            $s = new Test;
            for ($i = 0; $i < $iteration; $i++) {
                $obj = new stdClass;
                array_push($s->data, $obj);
            }
            break;  
        default:
            echo 'Type in ?test=#';
    }
    
    $time = microtime();
    $time = explode(' ', $time);
    $time = $time[1] + $time[0];
    $finish = $time;
    $total_time = round(($finish - $start), 6);
    echo 'Page generated in '.$total_time.' seconds.';
    

提交回复
热议问题