X

echo:comma vs dot

(Last Updated On: 2018年8月13日)

昨日、echo ‘abc’,’xyz’  (カンマ) と echo ‘abc’.’xyz’ (ドット) とどちらが速い?と言う話になったので簡単な実験をしてみました。

PHPのソースコードを見るとどちらも有利な点と不利な点があります。随分前に試した時も一般にカンマの方が速いと思える結果でしたがPHP5.3ではどうなのか試してみました。

コード

<?php
$text = '
Prevent possible crash when joining to a scalar function
Prevent transitory data corruption of GIN indexes after a crash
Prevent data corruption on TOAST columns when copying data
Fix failures during hot standby startup
Correct another "variable not found in subplan target list" bug
Fix bug with sorting on aggregate expressions in windowing functions
Multiple bug fixes for pg_upgrade
Change Foreign Key creation order to better support self-referential keys**
Multiple bug fixes to CREATE EXTENSION
Ensure that function return type and data returned from PL/perl agree
Ensure that PL/perl strings are always UTF-8
Assorted bug fixes for various Extensions
Updates to the time zone database, particularly to CST6
Changes marked with ** above require additional, post-update steps in order to fix all described issues. See the release notes for each version for a full list of changes with details of the fixes and steps.
One-click installer, including Windows packages';

$ary = split(' ', $text);
if (!empty($_GET['c'])) {
$scr = 'echo \''.join('\',\'', $ary).'\';';
} else {
$scr = 'echo \''.join('\'.\'', $ary).'\';';
}
echo $scr;
for ($i=0; $i<100; $i++) {
eval($scr);
}

 

速度差が分かりやすいように100回出力を繰り返しています。これをabで実行してみました。

このケースの場合、

ドット
Requests per second:    459.25 [#/sec] (mean)

カンマ
Requests per second:    582.24 [#/sec] (mean)

となり、カンマの方がおよそ27%ほど速い結果となりました。

試していませんがechoで出力する文字列が短く、文字列連結が少ない場合、連結のオーバーヘッドが小さいのでここまでの違いは出ないか、もしかすると速くなるケースもあると思います。ただ、恐らく一般のアプリケーションはドットで連結してから出力するより、カンマで連結なしで出力した方が速くなるケースの方が多いと思われます。

CMSなどで実験すると分かりやすいのですが、流石に書き換えが結構大変。。。

どなたか実験された方がいらしたら教えてください。

 

abの実行結果: ドット

[yohgaki@dev ~]$ ab -c 100 -n 10000 http://192.168.100.51/echo.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.100.51 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests

Server Software:        Apache/2.2.15
Server Hostname:        192.168.100.51
Server Port:            80

Document Path:          /echo.php
Document Length:        84947 bytes

Concurrency Level:      100
Time taken for tests:   21.774 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      851290000 bytes
HTML transferred:       849470000 bytes
Requests per second:    459.25 [#/sec] (mean)
Time per request:       217.745 [ms] (mean)
Time per request:       2.177 [ms] (mean, across all concurrent requests)
Transfer rate:          38179.50 [Kbytes/sec] received

Connection Times (ms)
min  mean[+/-sd] median   max
Connect:        0    0   0.6      0       5
Processing:     8  217 113.1    212    1033
Waiting:        1   64  79.0     13     218
Total:          8  217 113.1    213    1033

Percentage of the requests served within a certain time (ms)
50%    213
66%    227
75%    250
80%    271
90%    364
95%    439
98%    533
99%    599
100%   1033 (longest request)

abの実行結果: カンマ

[yohgaki@dev ~]$ ab -c 100 -n 10000 http://192.168.100.51/echo.php?c=1
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.100.51 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests

Server Software:        Apache/2.2.15
Server Hostname:        192.168.100.51
Server Port:            80

Document Path:          /echo.php?c=1
Document Length:        84947 bytes

Concurrency Level:      100
Time taken for tests:   17.175 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      851290000 bytes
HTML transferred:       849470000 bytes
Requests per second:    582.24 [#/sec] (mean)
Time per request:       171.751 [ms] (mean)
Time per request:       1.718 [ms] (mean, across all concurrent requests)
Transfer rate:          48403.74 [Kbytes/sec] received

Connection Times (ms)
min  mean[+/-sd] median   max
Connect:        0    0   0.9      0       7
Processing:     6  171 140.9    145    1388
Waiting:        1   17  20.6      6     375
Total:          6  172 141.0    146    1388

Percentage of the requests served within a certain time (ms)
50%    146
66%    175
75%    205
80%    233
90%    340
95%    444
98%    595
99%    705
100%   1388 (longest request)

Tags: PHP
yohgaki:
Related Post
Leave a Comment