PHPのテンプレートエンジンといえばSmartyですが、JinjaスタイルのテンプレートのTwigでベンチマークをとってみました。
TwigにはC拡張モジュール版があり、C拡張モジュールでパフォーマンスの向上を期待したのですが・・・。
なお、ベンチマークに使ったのは、DBのテーブルのデータを一覧表示するというよくあるアプリケーションです。
Smarty vs. Twig
本番環境ではテンプレートをコンパイルしないで使うことは殆どないと思いますので、コンパイル済みテンプレートで比較しました。今回使用したテンプレートはそれほど複雑ではないので、生成されたPHPのコードにさほど大きな違いはなく、互角でした。
Smarty
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
Concurrency Level: 1 Time taken for tests: 23.903 seconds Complete requests: 1000 Failed requests: 995 (Connect: 0, Receive: 0, Length: 995, Exceptions: 0) Write errors: 0 Total transferred: 29401850 bytes HTML transferred: 28988850 bytes Requests per second: 41.84 [#/sec] (mean) Time per request: 23.903 [ms] (mean) Time per request: 23.903 [ms] (mean, across all concurrent requests) Transfer rate: 1201.23 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.0 0 0 Processing: 6 24 3.5 24 115 Waiting: 6 24 3.4 24 115 Total: 6 24 3.5 24 115 Percentage of the requests served within a certain time (ms) 50% 24 66% 24 75% 24 80% 24 90% 25 95% 25 98% 25 99% 26 100% 115 (longest request) |
Twig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
Concurrency Level: 1 Time taken for tests: 24.061 seconds Complete requests: 1000 Failed requests: 999 (Connect: 0, Receive: 0, Length: 999, Exceptions: 0) Write errors: 0 Total transferred: 34750023 bytes HTML transferred: 34337023 bytes Requests per second: 41.56 [#/sec] (mean) Time per request: 24.061 [ms] (mean) Time per request: 24.061 [ms] (mean, across all concurrent requests) Transfer rate: 1410.40 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.0 0 0 Processing: 8 24 7.7 27 110 Waiting: 8 24 7.6 27 110 Total: 8 24 7.7 27 110 Percentage of the requests served within a certain time (ms) 50% 27 66% 28 75% 29 80% 29 90% 29 95% 29 98% 30 99% 31 100% 110 (longest request) |
Twig vs. Twig(C拡張)
上のベンチマークをとって少し時間があいたので再度Twigのベンチマークもとって比較してみました。※C拡張のインストール方法はこちら
結果はご覧の通りで、ほとんど違いはありませんでした。複雑なテンプレートであれば15パーセント程度高速になるという報告もありますが、通常の使用であればC拡張を使う必要はないと思います。
Twig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
Concurrency Level: 1 Time taken for tests: 27.053 seconds Complete requests: 1000 Failed requests: 6 (Connect: 0, Receive: 0, Length: 6, Exceptions: 0) Write errors: 0 Total transferred: 34757820 bytes HTML transferred: 34344820 bytes Requests per second: 36.97 [#/sec] (mean) Time per request: 27.053 [ms] (mean) Time per request: 27.053 [ms] (mean, across all concurrent requests) Transfer rate: 1254.71 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.0 0 0 Processing: 8 27 3.4 28 46 Waiting: 8 27 3.4 28 46 Total: 8 27 3.4 28 46 Percentage of the requests served within a certain time (ms) 50% 28 66% 29 75% 29 80% 29 90% 29 95% 29 98% 30 99% 32 100% 46 (longest request) |
Twig(C拡張)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Concurrency Level: 1 Time taken for tests: 26.801 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 34758000 bytes HTML transferred: 34345000 bytes Requests per second: 37.31 [#/sec] (mean) Time per request: 26.801 [ms] (mean) Time per request: 26.801 [ms] (mean, across all concurrent requests) Transfer rate: 1266.49 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.0 0 0 Processing: 7 27 8.2 28 230 Waiting: 7 27 8.2 28 230 Total: 7 27 8.2 29 230 Percentage of the requests served within a certain time (ms) 50% 29 66% 29 75% 29 80% 29 90% 29 95% 29 98% 30 99% 31 100% 230 (longest request) |
C拡張モジュールのインストール
1 2 3 4 5 6 |
$ git clone git://github.com/fabpot/Twig.git $ cd Twig/ext/twig $ /usr/bin/phpize $ ./configure --with-php-config=/usr/bin/php-config $ make $ sudo make install |
php.iniに以下の行を追加してWebサーバーを再起動します。
1 |
extension=twig.so |
phpinfo()の出力に以下の項目が表示されればインストール完了です。