php Type declarations

このところ、JavascriptTypescript やら 型にガチガチにきびしい Rust なんかをいじっていると、結果、これらの書き方も一度は関わって知っておいた方がなにかと良かったと思ってはいる。特に Rust なんかと格闘していると、いやでも変数やら引数やら戻り値なんかの型を考えることが身についてくる。

そういえば php でも 7.? あたりから型宣言ができるようになっていたのではなかったか、と。ふと、思ったのは、こちらで型指定をすることで、php 側で型推定しなくて済むので、その分が速くなったりしないのだろうかと。いや、型チェックが入る分、逆に負荷が増えて遅くなってしまうのだろうか?と。

これは意外に型宣言することで速くなってくれるのであれば、よろこんで型宣言するのだけれど。などと考えていると、ちょっとわくわくしてくるし、試してみないことには始まらないのです。
php の場合、型宣言ができるといっても、いまのところ、それは関数においての引数と戻り値に限ってのことのようではあるけれど。あぁ、あとクラスのプロパティもだったか。

とりあえずは、関数においての型宣言を試してみるってことで。簡単なベンチを書いてやってみる。型宣言有りと無しの関数を 10,000回 ループさせて計測したのを 5 回づつやった平均を出して、それの平均値を10回分データをとって比較してみた。ちなみに型宣言無しの場合、declare 宣言の影響は無いとは思うものの、一応確認してみると、declare 宣言があってもなくても結果に違いはなかった。

<?php
    declare ( strict_types = 1 );
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Type declarations benchmark test</title>
<meta http-equiv="Cache-Control" content="no-cache" />
</head>

<body>
	<div id="maincontents">
        <?php
           // 型宣言有り関数
            function add_int( int $a, int $b ): string {
                return ( string ) ( $a + $b );
            }

           // 型宣言無し関数
            function adds_int( $a, $b ) {
                return ( string) ( $a + $b );
            }

            $ansa = array();
            $ansb = array();

            echo '<p> Type 無し 10,000回</p>';
            for ( $i = 0; $i < 5; ++$i ) {
                $gettime[0] = microtime( true );

                for( $j = 1; $j < 10000; ++$j ) {

                    $x = mt_rand();
                    $y  = mt_rand();

                    $z = adds_int( $x, $y );
                }
                $gettime[1] = microtime( true );
                $ans = $gettime[1] - $gettime[0];
                echo '<p>' . $i . '回目 : ' . sprintf( '%0.5f', $ans ) . '秒</p>';

                $ansa[] = $ans;
            }
            echo '<p>' . sprintf( '%0.5f', array_sum( $ansa ) / count( $ansa ) ) . '</p>';

            echo '<p> Type 有り 10,000回</p>';
            for ( $i = 0; $i < 5; ++$i ) {
                $gettime[0] = microtime( true );

                for( $j = 1; $j < 10000; ++$j ) {

                    $x = mt_rand();
                    $y  = mt_rand();

                    $z = add_int( $x, $y );
                }
                $gettime[1] = microtime( true );
                $ans = $gettime[1] - $gettime[0];
                echo '<p>' . $i . '回目 : ' . sprintf( '%0.5f', $ans ) . '秒</p>';

                $ansb[] = $ans;
            }
            echo '<p>' . sprintf( '%0.5f', array_sum( $ansb ) / count( $ansb ) ) . '</p>';
        ?>
	</div>
</div>

</body>
</html>
PHP
CopyExpand

結果は以下のごとし。

型宣言なし 型宣言あり .00149 .00148 .00145 .00163 .00146 .00151 .00142 .00168 .00152 .00146 .00157 .00162 .00142 .00157 .00156 .00164 .00164 .00144 .00179 .00144 .00153 – average .00155 – average

と、いうことになった。
早かったり遅かったりでほとんど変わらないよう。まぁ、どちらでもそんなに変わらないということのようで。速度的にはお好きなほうで書いてくださいってところか。まぁ、なら型宣言ありで書いたほうがわかりやすいし、良いということになるのでは。

Leave a Reply!

JavaScript is necessary to send a comment.
You can edit and delete your comment if you input a edit key.
Edit key is necessary for attesting you when you edit and delete it.
The tag of HTML cannot be used in comment.
When you comment for the first time, it is displayed after the approval of the administrator.
Because I cannot speak English so much, it takes time to answer.
Required fields are marked *.

※Please enter more than 5 characters only alphabets.
※Edit or delete are possible for 2000 days after approval.

*

♠Simplistic Comment User Editable v4.0

♠When visitors leave comments on the site this site collect the data shown in the comments form, and also the visitor’s IP address and browser user agent string to help spam detection.
♠This site does not use cookie when visitors leave comments and commenter edit comment.
♠This site uses Akismet to reduce spam. Learn how your comment data is processed.

Comments feed

Trackback URL : https://strix.main.jp/wp-trackback.php?p=172491

Sanbanse Funabashi
2010.10.24 sunrise

Top

スクロールさせるか画像をクリックすると元に戻ります。