それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

php のシングルクォートはほんとうに早いのか

シングルクォートは早い、ダブルクオーテーションに比べて早いという話がありますが。なので変数展開が必要じゃない場面ではシングルを使いましょうとかそういうルールをよく見かけます。

本当なんでしょうか。

先に結論を書くと

殆ど変わらないので、読みやすさ、書きやすさ、コーディング規約を優先した方がいいよ

ってことです。http://www.phpbench.com/ に買いてありました。

In today's versions of PHP it looks like this argument has been satisfied on both sides of the line. Lets all join together in harmony in this one!

シングル・ダブルも変わらないんだし、便利に使えばイイじゃん。

本当なんでしょうか。

理系なので実験しましょうか。

<?php

foreach(range(1,1000*1000) as $cnt ){
     $str = "人は「考え方」を手に入れたとたん頭のよくなる生き物である";
}

結果

takuya@air:~/Desktop$ php strings.php
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in /Users/takuya/Desktop/strings.php on line 3

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in /Users/takuya/Desktop/strings.php on line 3
takuya@air:~/Desktop$ php strings.php
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in /Users/takuya/Desktop/strings.php on line 3

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in /Users/takuya/Desktop/strings.php on line 3
|

いきなりのメモリエラー
ダブルクオーテーションもシングルクォーテーションもメモリ不足(´・ω・`)

さて、たった128MB(134217728 bytes)のStringが足りなくなるので

メモリ増量してやり直そう。
 463 ; Maximum amount of memory a script may consume (128MB)
 464 ; http://php.net/memory-limit
 465 memory_limit = 1024M
 466

メモリが足りなくなるってことは、、、
つまり、パフォーマンスの差が出る前にメモリ不足になるってことじゃん・・・

でも、いまはパフォーマンスの差を見るんだ。

再挑戦した

気を取り直して、測定した。

シングルクォート
takuya@air:~/Desktop$ time php strings.php

real     0m0.635s
user     0m0.496s
sys     0m0.124s
ダブルクォーテーション
takuya@air:~/Desktop$ time php strings.php

real     0m0.680s
user     0m0.533s
sys     0m0.144s


変わりましたよ、0.045s も変わりました

45msも差がでたよ。

45ms は早くなりすぎじゃん・・・

あれ。。ダブルクォーテーション→シングルクォートの変換で 45ms も早くなる?

1000*1000 個の文字列確保をやったわけだから、 100万個の文字列宣言があった所で 45ms ですね、

ダブルクォーテーションをシングル変えて得られる速度は?

0.000000045 秒 / 1シングルクオート に取り替え

で、これって本当に、比例してるのかちょっと不安なので念の為

1000回で比較(念のため)

シングル

takuya@air:~/Desktop$ time php strings.php

real     0m0.068s
user     0m0.054s
sys     0m0.011s

ダブル

takuya@air:~/Desktop$ time php strings.php

real     0m0.072s
user     0m0.057s
sys     0m0.012s
0.00004 秒 

比例してない・・・

若干差があるのか?

明らかにパフォーマンスが変わりそうな、1000*1000*1000で試しました。

では、10ギガ回(10億) 行きましょう

 <?php

 foreach(range(1,1000*1000*1000) as $cnt ){
     $str = "人は「考え方」を手に入れたとたん頭のよくなる生き物である";
 }
takuya@air:~/Desktop$ time php strings.php
PHP Fatal error:  Allowed memory size of 1073741824 bytes exhausted (tried to allocate 71 bytes) in /Users/takuya/Desktop/strings.php on line 3

Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 71 bytes) in /Users/takuya/Desktop/strings.php on line 3

real     0m14.607s
user     0m1.448s
sys     0m1.205s

はいおつかれ。メモリエラー。。

パフォーマンスに差が出る頃にはメモリが足りない。

パフォーマンスに影響するレベルにダブルクォーテーションとシングルクォートが速度に違いが出るころには、文字列がメモリに収まらないのでレベルですね。

やっぱり気にすること無いんじゃん。

おまけ:変数展開した場合で試す。

変数展開があるとどうなるんでしょう。

<?php
$b = 1;
foreach(range(1,1000) as $cnt ){
     //$str = "${b} is greator than 0;";
}

何もしない時(ループだけ)

コメントアウトして、ループだけ

takuya@air:~/Desktop$ time php strings.php

real     0m0.066s
user     0m0.052s
sys     0m0.011s

シングルクォートで文字列を結合

ダブルクオートが遅いなら、シングル結合したら早い?

takuya@air:~/Desktop$ time php strings.php

real     0m0.070s
user     0m0.054s
sys     0m0.012s

ダブクオートで文字列を展開

takuya@air:~/Desktop$ time php strings.php

real     0m0.070s
user     0m0.054s
sys     0m0.011s


変わらない。シングルでつなごうが、ダブルクオーテーションで変数展開しようが殆ど変わらない。

StackOverFlowに書いてあるとおり

As for speed, the only right answer is it shouldn't be your concern at all. Period. The difference, if you even find one, doesn't matter at all. To be concerned of speed, one should understand whole picture, not one pixel of it. Making your application faster and more efficient is a great and complicated task. But it cannot be done by asking "which is faster" questions. If you really concerned in that, start from learning what profiling is.

見やすい方を使ったらいいし、どっちかに統一したほうが見栄えが綺麗だよと


だったら、シングルクォートとダブルクォーテーションが混在しているより、全部のコードがダブルクォーテーションに統一しててたほうが

一括置換の時に便利ですよねって話

phpbenchに書いてる。

http://www.phpbench.com/

phpbench にあるように、シングルクォートでもダブルクォーテーションでもどっちの立場でも充分なパフォーマンスは出てるんだから、そんな議論せずに、仲良くやろうぜ。使いやすいように使えばいいじゃん。

同感です。




まとめ

気にするほど変わるとは思えない。ぶっちゃけ、混ぜるほうが面倒だ。

WEBの情報を鵜呑みのするのは、、、可能性はゼロではないでしょうか、

USキーボードならシングルクォートの方が打つのが早いので(Shift不要)。だからシングルダブルクオーテーションをどちらを使うかといえば、シングルクオーテーションが選ばれる傾向があるように思います。
 日本語キーだとあまり変わらないのですが、シングル(7)よりだぶる(2)の方が押しやすそうな気がします。

参考資料

http://object.nekousa.com/?p=569
http://docolog.cocolog-nifty.com/papalog/2012/02/phpok-c6b8.html
http://stackoverflow.com/questions/4871065/what-is-faster-in-php-single-or-double-quotes




シングル・ダブルの両方使っていると

変数展開不要だったけど、必要になったからシングルクォートをダブルクォーテーションに書きなおし
変数展開してたけど、不要になったからダブルクォーテーションをシングルクォートに書きなし

と考えること増えるので、細かいことを気にしない富豪プログラマはダブルクォーテーション統一でいいんじゃないでしょうか。


変数展開に限ればシングルとかもPEARで見たような気がする。

define (HOGEHOGE,'/var/www/my/host');

みたいな定数に限れば、シングルクォートしてたら定数ってルールもありですかねぇ。

define( "$hostname", "http://$server"  );

みたいに 書くこともあるでしょうから一概には言えないでしょうが