เปรียบเทียบความเร็วในการใช้งานฟังก์ชั่น empty() และ isset()

บทความนี้เป็นการทดสอบเปรียบเทียบความเร็วในการใช้งานฟังก์ชั่น empty() และ isset() กับตัวแปรประเภทต่างๆ

ผลการทดสอบบน PHP 4 เวอร์ชั่นบนเครื่องเดียวกัน

Current PHP version: 5.2.17
empty isset
string ว่างเปล่า 0.013168776035309 0.012722826004028
ตัวเลข 0 0.01331479549408 0.012506699562073
แอเรย์ ว่างเปล่า 0.013162767887115 0.012456941604614
string ไม่ว่าง 0.013111460208893 0.012779915332794
ตัวเลข 1 0.012525880336761 0.012289130687714
แอเรย์ มีสมาชิก 0.01272269487381 0.012173902988434
ไม่มีตัวแปร 0.0088480114936829 0.0088397860527039

Current PHP version: 5.4.45
empty isset
string ว่างเปล่า 0.0056337594985962 0.0069453954696655
ตัวเลข 0 0.0071183800697327 0.0066091299057007
แอเรย์ ว่างเปล่า 0.006779956817627 0.0064022779464722
string ไม่ว่าง 0.0059184432029724 0.0061661601066589
ตัวเลข 1 0.0060444116592407 0.0081516623497009
แอเรย์ มีสมาชิก 0.0062620759010315 0.0066883444786072
ไม่มีตัวแปร 0.0067680478096008 0.0072224020957947

Current PHP version: 5.6.14
empty isset
string ว่างเปล่า 0.0053485035896301 0.0050637006759644
ตัวเลข 0 0.0052383542060852 0.0049368143081665
แอเรย์ ว่างเปล่า 0.005267345905304 0.0050349116325378
string ไม่ว่าง 0.0047216296195984 0.004958975315094
ตัวเลข 1 0.0045747756958008 0.0048004508018494
แอเรย์ มีสมาชิก 0.0045584678649902 0.0047675609588623
ไม่มีตัวแปร 0.0051348686218262 0.0046244025230408

Current PHP version: 7.1.0-dev
empty isset
string ว่างเปล่า 0.0026311993598938 0.0022942423820496
ตัวเลข 0 0.0025519609451294 0.0022835612297058
แอเรย์ ว่างเปล่า 0.002529239654541 0.0022364735603333
string ไม่ว่าง 0.0027160167694092 0.0022122144699097
ตัวเลข 1 0.0025909543037415 0.0021446228027344
แอเรย์ มีสมาชิก 0.0025252223014832 0.0021265029907227
ไม่มีตัวแปร 0.0021593451499939 0.0023514270782471

จากผลการทดสอบ จะเห็นว่าทั้ง empty และ isset ไม่ต่อยแตกต่างกันมากนักนะครับ

โค้ดที่ใช้ในการทดสอบ
// จำนวนครั้งการทดสอบ
$max = 100000;
// จำนวนรอบการทดสอบ ผลลัพท์เฉลี่ยจากค่านี้
$count = 20;
// แอเรย์เก็บผลลัพท์
$ret = array();
// ข้อมูล
$s = '';
$ss = 's';
$n = 0;
$nn = 1;
$a = array();
$aa = array(1, 2, 3);
$ret = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,);
for ($m = 0; $m < $count; $m++) {
    // string ว่างเปล่า
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        if (empty($s)) {

        }
    }
    $ret[0] += microtime(true) - $start;
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        if (isset($s)) {

        }
    }
    $ret[1] += microtime(true) - $start;

    // ตัวเลข 0
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        if (empty($n)) {

        }
    }
    $ret[2] += microtime(true) - $start;
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        if (isset($n)) {

        }
    }
    $ret[3] += microtime(true) - $start;

    // แอเรย์ ว่างเปล่า
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        if (empty($a)) {

        }
    }
    $ret[4] += microtime(true) - $start;
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        if (isset($a)) {

        }
    }
    $ret[5] += microtime(true) - $start;

    // string ไม่ว่าง
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        if (empty($ss)) {

        }
    }
    $ret[6] += microtime(true) - $start;
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        if (isset($ss)) {

        }
    }
    $ret[7] += microtime(true) - $start;

    // ตัวเลข 1
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        if (empty($nn)) {

        }
    }
    $ret[8] += microtime(true) - $start;
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        if (isset($nn)) {

        }
    }
    $ret[9] += microtime(true) - $start;

    // แอเรย์ มีสมาชิก
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        if (empty($aa)) {

        }
    }
    $ret[10] += microtime(true) - $start;
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        if (isset($aa)) {

        }
    }
    $ret[11] += microtime(true) - $start;

    // ไม่มีตัวแปร
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        if (empty($null)) {

        }
    }
    $ret[12] += microtime(true) - $start;
    $start = microtime(true);
    for ($z = 0; $z < $max; $z++) {
        if (isset($null)) {

        }
    }
    $ret[13] += microtime(true) - $start;

    usleep(10240);
}
echo 'Current PHP version: '.phpversion().'<br>';
echo '<table class="data border">';
echo '<tr><th></th><th>empty</th><th>isset</th></tr>';
echo '<tr><th>string ว่างเปล่า</th><td>'.($ret[0] / $count).'</td><td>'.($ret[1] / $count).'</td></tr>';
echo '<tr><th>ตัวเลข 0</th><td>'.($ret[2] / $count).'</td><td>'.($ret[3] / $count).'</td></tr>';
echo '<tr><th>แอเรย์ ว่างเปล่า</th><td>'.($ret[4] / $count).'</td><td>'.($ret[5] / $count).'</td></tr>';
echo '<tr><th>string ไม่ว่าง</th><td>'.($ret[6] / $count).'</td><td>'.($ret[7] / $count).'</td></tr>';
echo '<tr><th>ตัวเลข 1</th><td>'.($ret[8] / $count).'</td><td>'.($ret[9] / $count).'</td></tr>';
echo '<tr><th>แอเรย์ มีสมาชิก</th><td>'.($ret[10] / $count).'</td><td>'.($ret[11] / $count).'</td></tr>';
echo '<tr><th>ไม่มีตัวแปร</th><td>'.($ret[12] / $count).'</td><td>'.($ret[13] / $count).'</td></tr>';
echo '</table>';
ผู้เขียน goragod โพสต์เมื่อ 11 ธ.ค. 2558 เปิดดู 3,523 ป้ายกำกับ benchmark
^