【PHP】条件判定のベンチマーク

PHP

PHPの条件判定処理についてベンチマークテストを実施してみました。

対象の条件判定処理は、if文、三項演算子、switch文、match式の4種類です。

対象のPHPバージョンは、8.2 / 8.3 / 8.4 です。

▼テストケース:

$value = 'delete';

という変数について、4回目の条件判定で true となるようにします。

ちなみに、switch文の条件判定は型チェックをしない緩い比較「==」のため、if文と三項演算子も緩い比較にします。

型チェックを含めると若干遅くなります。

1000000(ひゃくまん)回イテレーションさせて時間計測します。

▼if文

        if ($value == 'insert') {
            return 'insert';
        } elseif ($value == 'select') {
            return 'select';
        } elseif ($value == 'update') {
            return 'update';
        } elseif ($value == 'delete') { // ここでtrue
            return 'delete';
        } else {
            return '';
        }

▼三項演算子:

        return $value == 'insert'
            ? 'insert'
            : ($value == 'select'
                ? 'select'
                : ($value == 'update'
                    ? 'update'
                    : ($value == 'delete' // ここでtrue
                        ? 'delete'
                        : ''
                    )
                )
            );

※通常、ここまでネストすることは無いでしょう。

▼switch文:

        switch ($value) {
            case 'insert':
                return 'insert';
                break;
            case 'select':
                return 'select';
                break;
            case 'update':
                return 'update';
                break;
            case 'delete': // ここでtrue
                return 'delete';
                break;
            default:
                return '';
        }

▼match式

        return match ($value) {
            'insert' => 'insert',
            'select' => 'select',
            'update' => 'update',
            'delete' => 'delete', // ここでtrue
            default => '',
        };

▼ベンチマークテストに使用したツール:

GitHub - macocci7/PHP-Benchmark: Simple Benchmark script for PHP.
Simple Benchmark script for PHP. Contribute to macocci7/PHP-Benchmark development by creating an account on GitHub.

▼テストコード

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Macocci7\PhpBenchmark\Benchmark;

$value = 'delete';

Benchmark::codes([
    'if' => function () use ($value) {
        if ($value == 'insert') {
            return 'insert';
        } elseif ($value == 'select') {
            return 'select';
        } elseif ($value == 'update') {
            return 'update';
        } elseif ($value == 'delete') { // ここでtrue
            return 'delete';
        } else {
            return '';
        }
    },
    'ternary operator' => function () use ($value) {
        return $value == 'insert'
            ? 'insert'
            : ($value == 'select'
                ? 'select'
                : ($value == 'update'
                    ? 'update'
                    : ($value == 'delete' // ここでtrue
                        ? 'delete'
                        : ''
                    )
                )
            );
    },
    'switch' => function () use ($value) {
        switch ($value) {
            case 'insert':
                return 'insert';
                break;
            case 'select':
                return 'select';
                break;
            case 'update':
                return 'update';
                break;
            case 'delete': // ここでtrue
                return 'delete';
                break;
            default:
                return '';
        }
    },
    'match' => function () use ($value) {
        return match ($value) {
            'insert' => 'insert',
            'select' => 'select',
            'update' => 'update',
            'delete' => 'delete', // ここでtrue
            default => '',
        };
    },
], 1000000);

▼実行環境:

  • PHP Version: 8.2.28 / 8.3.20 / 8.4.6
  • OS: Ubuntu24.04.2 LTS on WSL2 (Windows11 26100.3775)
  • Machine: AMD Ryzen 7 7730U(2.00GHz) / RAM 16GB / 64bit

▼実行結果:

・PHP 8.2

1:               if => Time: 0.031544 sec  Avg: 0.0000000315 sec
2: ternary operator => Time: 0.036023 sec  Avg: 0.0000000360 sec
3:           switch => Time: 0.018259 sec  Avg: 0.0000000183 sec
4:            match => Time: 0.020085 sec  Avg: 0.0000000201 sec

・PHP 8.3

1:               if => Time: 0.032325 sec  Avg: 0.0000000323 sec
2: ternary operator => Time: 0.035866 sec  Avg: 0.0000000359 sec
3:           switch => Time: 0.022911 sec  Avg: 0.0000000229 sec
4:            match => Time: 0.024265 sec  Avg: 0.0000000243 sec

・PHP 8.4

1:               if => Time: 0.032125 sec  Avg: 0.0000000321 sec
2: ternary operator => Time: 0.033554 sec  Avg: 0.0000000336 sec
3:           switch => Time: 0.018922 sec  Avg: 0.0000000189 sec
4:            match => Time: 0.021002 sec  Avg: 0.0000000210 sec

▼評価

各バージョンとも同じ傾向となりました。

速い順に、

1.switch文

2.match式

3.if文

4.三項演算子(多重ネスト)

となりました。

▼1回目の条件判定で true となるように変えてみる

ちなみに、

$value = 'insert';

として、1回目の条件判定で true となるようにしてみます。

・PHP 8.2

1:               if => Time: 0.016145 sec  Avg: 0.0000000161 sec
2: ternary operator => Time: 0.017214 sec  Avg: 0.0000000172 sec
3:           switch => Time: 0.018625 sec  Avg: 0.0000000186 sec
4:            match => Time: 0.020325 sec  Avg: 0.0000000203 sec

・PHP 8.3

1:               if => Time: 0.015971 sec  Avg: 0.0000000160 sec
2: ternary operator => Time: 0.016566 sec  Avg: 0.0000000166 sec
3:           switch => Time: 0.023038 sec  Avg: 0.0000000230 sec
4:            match => Time: 0.024209 sec  Avg: 0.0000000242 sec

・PHP 8.4

1:               if => Time: 0.015743 sec  Avg: 0.0000000157 sec
2: ternary operator => Time: 0.016868 sec  Avg: 0.0000000169 sec
3:           switch => Time: 0.018235 sec  Avg: 0.0000000182 sec
4:            match => Time: 0.021555 sec  Avg: 0.0000000216 sec

1回目の判定で true となる場合は、早い順に、

1.if文

2.三項演算子

3.switch文

4.match式

となっています。

if文と三項演算子の比較演算子を、

「==」から「===」に変えてみても、同様の結果でした。

型チェックを含めても、if文と三項演算子はswitch文より速いです。

▼まとめ

elseif が少ない浅めのif文で攻めるのが最速。

ネストしなけりゃ三項演算子は速い。

筆者はswitch文が嫌いなので使いたくないです。。

コメント

タイトルとURLをコピーしました