ทดสอบความเร็วในการสร้างอ๊อปเจ็ค

ไม่มีอะไรมากครับ แค่ผมสงสัยว่า ถ้าต้องการจะสร้างอ๊อปเจ็คแบบไดนามิค จะใช้ วิธีไหนในการสร้างจะมีประสิทธิภาพมากที่สุด

วิธีแรก เป็นการสร้างอ๊อปเจ็คโดยการสร้างเป็น dynamic array ก่อน แล้วค่อยแปลงเป็นอ๊อปเจ็คในภายหลัง
$result = array();
$result['topic'] = 'Sorry, can not find a page called Please check the URL or try the call again.';
$result['detail'] = '<div class=error>Sorry, can not find a page called Please check the URL or try the call again.</div>';
$result = (object)$result;

วิธีที่สอง เป็นการสร้างอ๊อปเจ็คจากแอเรย์อีกรูปแบบหนึ่ง แล้วค่อยแปลงเป็นอ๊อปเจ็คในภายหลังเช่นกัน
$result = array(
     'topic' => 'Sorry, can not find a page called Please check the URL or try the call again.',
     'detail' => '<div class=error>Sorry, can not find a page called Please check the URL or try the call again.</div>'
);
$result = (object)$result;

วิธีที่สาม เป็นการสร้างอ๊อปเจ็คด้วย stdClass
$result = new stdClass();
$result->topic = 'Sorry, can not find a page called Please check the URL or try the call again.';
$result->detail = '<div class=error>Sorry, can not find a page called Please check the URL or try the call again.</div>';

ผมมีผลการทดสอบกับ PHP 4 เวอร์ชั่นบนเครื่องเดียวกันมาฝาก
Current PHP version: 5.2.17
0.0012758016586304 (dynamic array)
0.0011265873908997 (static array)
0.0013129353523254 (stdClass)

Current PHP version: 5.4.45
0.0010268092155457 (dynamic array)
0.00083678960800171 (static array)
0.0007853627204895 (stdClass)

Current PHP version: 5.6.14
0.0010161519050598 (dynamic array)
0.00089441537857056 (static array)
0.00079135894775391 (stdClass)

Current PHP version: 7.1.0-dev
0.00039738416671753 (dynamic array)
0.00032914876937866 (static array)
0.00035353899002075 (stdClass)

โค้ดที่ใช้ในการทดสอบ
// จำนวนครั้งการทดสอบ
$max = 1000;
// จำนวนรอบการทดสอบ ผลลัพท์เฉลี่ยจากค่านี้
$count = 20;
$n = 0;
$o = 0;
$p = 0;
for ($m = 0; $m < $count; $m++) {
    // วิธีแรก เป็นการสร้างอ๊อปเจ็คโดยการสร้างเป็น dynamic array ก่อน แล้วค่อยแปลงเป็นอ๊อปเจ็คในภายหลัง
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        $result = array();
        $result['topic'] = 'Sorry, can not find a page called Please check the URL or try the call again.';
        $result['detail'] = '<div class=error>Sorry, can not find a page called Please check the URL or try the call again.</div>';
        $result = (object)$result;
    }
    $n += microtime(true) - $start;

    // วิธีที่สอง เป็นการสร้างอ๊อปเจ็คจากแอเรย์อีกรูปแบบหนึ่ง แล้วค่อยแปลงเป็นอ๊อปเจ็คในภายหลังเช่นกัน
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        $result = array(
            'topic' => 'Sorry, can not find a page called Please check the URL or try the call again.',
            'detail' => '<div class=error>Sorry, can not find a page called Please check the URL or try the call again.</div>'
        );
        $result = (object)$result;
    }
    $o += microtime(true) - $start;

    // วิธีที่สาม เป็นการสร้างอ๊อปเจ็คด้วย stdClass
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        $result = new stdClass();
        $result->topic = 'Sorry, can not find a page called Please check the URL or try the call again.';
        $result->detail = '<div class=error>Sorry, can not find a page called Please check the URL or try the call again.</div>';
    }
    $p += microtime(true) - $start;
    sleep(1);
}
echo 'Current PHP version: '.phpversion().'<br>';
echo ($n / $count).' (dynamic array)<br>';
echo ($o / $count).' (static array)<br>';
echo ($p / $count).' (stdClass)<br>';

 
ผู้เขียน goragod โพสต์เมื่อ 31 ต.ค. 2558 เปิดดู 2,720 ป้ายกำกับ benchmark
^