3v4l.org

run code in 300+ PHP versions simultaneously
[知恵袋を覗いていたら、書いてみたいコードがあったが、回答を書き込んでしまって質問を閉じられると、他の人のコードも見れないには嫌だなと思ったので、こちらにしれっと書いてみる。](http://qiita.com/ShibuyaKosuke/items/296a9007da4336d97848) token_get_allを使ってみたくて、関数を書いてみました。 細かくテストしてないので、バグがあったら教えてください( ´ ▽ ` )ノ ちなみにstackoverflowでも似たような質問がありました。 [Matching braces puzzle](http://stackoverflow.com/questions/18527972/matching-braces-puzzle) ```:PHP <?php /** * check matching of braces * * @param string $src target string to check * @return bool */ function brace_check($src) { $open_braces = str_split("({["); $close_braces = str_split(")}]"); $braces = array_merge($open_braces, $close_braces); // PHP open tagが含まれていないなら追加する $tokens = token_get_all($src); $open_tag_found = false; array_walk_recursive( $tokens, function($item,$key){ $open_tag_found |= $key === 0 && in_array($item,[T_OPEN_TAG,T_OPEN_TAG_WITH_ECHO]); }); if ( !$open_tag_found ){ // PHPコードとして処理 $tokens = token_get_all('<?php '.$src); } // bracesを含むであろう文字列トークン以外は捨てる $tokens = array_filter($tokens,function($item){ return is_string($item); }); $open_brace_stack = array(); foreach($tokens as $key => $t){ if (in_array($t,$open_braces)){ // 開括弧なら開括弧スタックに乗せる array_push( $open_brace_stack, $t); } elseif (in_array($t,$close_braces)){ if ( empty($open_brace_stack) ){ // 開括弧スタックが空ならエラー return false; } // 括弧の対応を確認(開括弧スタックのトップと現在の文字) $open_top = array_pop($open_brace_stack); $idx_open = array_search($open_top, $open_braces); $idx_close = array_search($t, $close_braces); if ( $idx_open !== $idx_close ){ return false; } } } return true; } //========================= // test code $html_case = <<<HTML <?php ?> <html> ... <ul> <li>1)...</li> <li>2)...</li> </ul> ... <?php ... ?> ... </html> HTML; $expressions = array(")(){}","[]({})","([])","{()[]}","([)]",$html_case); var_dump(array_map('brace_check', $expressions)); ```

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
5.6.90.0000.04720.99
5.6.80.0100.04020.55
5.5.250.0100.03320.68
5.5.240.0100.07720.28
5.4.410.0100.05318.96
5.4.400.0070.05318.78
5.4.390.0070.05318.64
5.4.380.0200.04718.79
5.4.370.0030.05718.79
5.4.360.0170.04718.54
5.4.350.0100.05718.62
5.4.340.0130.05018.61
5.4.320.0170.04718.76
5.4.310.0100.06718.73
5.4.300.0130.05018.79
5.4.290.0130.05018.73
5.4.280.0200.04018.53
5.4.270.0170.05318.73
5.4.260.0030.05718.73
5.4.250.0130.05018.54
5.4.240.0100.05318.57
5.4.230.0130.04718.78
5.4.220.0170.05018.70
5.4.210.0170.04718.47
5.4.200.0200.04016.57
5.4.190.0070.05318.81
5.4.180.0130.04718.77
5.4.170.0170.04718.52
5.4.160.0100.05018.70
5.4.150.0230.03718.61
5.4.140.0030.05716.23
5.4.130.0100.04716.29
5.4.120.0030.06016.29
5.4.110.0170.04016.41
5.4.100.0070.06316.43
5.4.90.0130.04716.18
5.4.80.0200.05316.27
5.4.70.0170.05716.26
5.4.60.0070.05316.16
5.4.50.0100.05316.18
5.4.40.0100.05316.16
5.4.30.0170.04316.19
5.4.20.0200.04316.32
5.4.10.0000.05716.29
5.4.00.0070.05015.75
5.3.290.0100.05714.65
5.3.280.0100.05314.73
5.3.270.0130.05314.79
5.3.260.0200.04314.74
5.3.250.0100.05314.63
5.3.240.0070.05714.72
5.3.230.0100.05714.71
5.3.220.0170.04314.75
5.3.210.0100.05314.64
5.3.200.0070.05714.76
5.3.190.0070.05314.75
5.3.180.0130.05014.67
5.3.170.0100.05314.58
5.3.160.0200.04314.69
5.3.150.0130.05014.73
5.3.140.0170.05714.74
5.3.130.0170.04714.68
5.3.120.0130.06714.77
5.3.110.0070.06014.60
5.3.100.0130.05014.06
5.3.90.0070.05314.02
5.3.80.0070.05314.04
5.3.70.0000.06014.02
5.3.60.0030.05714.13
5.3.50.0030.05714.16
5.3.40.0170.04314.17
5.3.30.0130.05313.93
5.3.20.0100.05013.75
5.3.10.0100.05013.66
5.3.00.0170.04313.87
5.2.170.0030.04711.36
5.2.160.0030.04311.13
5.2.150.0000.05011.27
5.2.140.0030.04711.16
5.2.130.0070.04011.14
5.2.120.0070.04011.13
5.2.110.0070.04011.15
5.2.100.0030.05011.21
5.2.90.0070.04011.13
5.2.80.0030.04711.21
5.2.70.0030.04711.12
5.2.60.0070.04311.26
5.2.50.0200.03011.13
5.2.40.0000.04711.01
5.2.30.0000.05311.07
5.2.20.0070.04011.16
5.2.10.0130.03310.99
5.2.00.0100.04010.90
5.1.60.0070.03310.08
5.1.50.0030.03710.07
5.1.40.0030.04010.00
5.1.30.0000.04710.37
5.1.20.0030.04010.41
5.1.10.0030.03710.11
5.1.00.0030.04010.10
5.0.50.0000.0338.75
5.0.40.0070.0338.42
5.0.30.0030.0438.23
5.0.20.0000.0308.33
5.0.10.0030.0308.31
5.0.00.0030.0438.35
4.4.90.0000.0275.90
4.4.80.0070.0205.98
4.4.70.0070.0235.88
4.4.60.0070.0206.01
4.4.50.0030.0235.93
4.4.40.0100.0305.92
4.4.30.0000.0275.88
4.4.20.0000.0275.95
4.4.10.0100.0175.98
4.4.00.0030.0335.92
4.3.110.0030.0235.83
4.3.100.0030.0235.82
4.3.90.0070.0175.83
4.3.80.0030.0375.85
4.3.70.0030.0205.91
4.3.60.0100.0135.89
4.3.50.0100.0175.93
4.3.40.0070.0305.77
4.3.30.0030.0274.56
4.3.20.0000.0234.54
4.3.10.0030.0204.50
4.3.00.0000.01316.51

preferences:
143.87 ms | 1394 KiB | 7 Q