0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

引数の数によるパフォーマンス比較

Last updated at Posted at 2018-06-19

引数の数によってパフォーマンスがどれくらい変化するかベンチを取りました。
PHP 7.2.5。使用したライブラリはPhpBenchmark。Xampp環境です。
コードは以下になります。

class Test
{
	public function args_0(){}

	public function args_1($a){}

	public function args_2($a, $b){}

	public function args_3($a, $b, $c){}

	public function args_4($a, $b, $c, $d){}
}

引数なし

Running tests 1000000 times. Testing 1/1 : args_0 Test Time Time (%) Memory Memory (%) args_0 126 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_0 Test Time Time (%) Memory Memory (%) args_0 123 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_0 Test Time Time (%) Memory Memory (%) args_0 125 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_0 Test Time Time (%) Memory Memory (%) args_0 130 ms 4 MB
平均 126 ms

引数1

Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 138 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 139 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 138 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 137 ms 4 MB
平均 138 ms

引数2

Running tests 1000000 times. Testing 1/1 : args_2 Test Time Time (%) Memory Memory (%) args_2 140 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_2 Test Time Time (%) Memory Memory (%) args_2 138 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_2 Test Time Time (%) Memory Memory (%) args_2 140 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_2 Test Time Time (%) Memory Memory (%) args_2 143 ms 4 MB
平均 140 ms

引数3

Running tests 1000000 times. Testing 1/1 : args_3 Test Time Time (%) Memory Memory (%) args_3 145 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_3 Test Time Time (%) Memory Memory (%) args_3 148 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_3 Test Time Time (%) Memory Memory (%) args_3 147 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_3 Test Time Time (%) Memory Memory (%) args_3 149 ms 4 MB
平均 147 ms

引数4

Running tests 1000000 times. Testing 1/1 : args_4 Test Time Time (%) Memory Memory (%) args_4 161 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_4 Test Time Time (%) Memory Memory (%) args_4 159 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_4 Test Time Time (%) Memory Memory (%) args_4 156 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_4 Test Time Time (%) Memory Memory (%) args_4 158 ms 4 MB
平均 158 ms

引数が増えれば増えるほどパフォーマンスが劣化することが分かりました。まぁ想定通りでしょう。

次はメソッドにデフォルト値を入れてみました。

引数1 デフォルト値あり

class Test2
{
	public function args_1($a = 0){}
}

Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 151 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 152 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 149 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 153 ms 4 MB
平均 151 ms

吃驚するほど遅くなりました。便利だからと積極的に使用するのは避けたほうが良さそうですね。

型宣言してみるとどうなるでしょう?

引数1 型宣言あり

class Test3
{
	public function args_1(int $a){}
}

Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 139 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 141 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 140 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 142 ms 4 MB
平均 140 ms

ありなしではパフォーマンス上、ほぼ変化が見られませんでした。
これなら可読性の向上という点で積極的に使用したほうが良さそうな気がします。

次は型宣言とデフォルト値をしてみました。

引数1 型宣言・デフォルト値あり

class Test4
{
	public function args_1(int $a = 0){}
}

Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 144 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 147 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 149 ms 4 MB
Running tests 1000000 times. Testing 1/1 : args_1 Test Time Time (%) Memory Memory (%) args_1 146 ms 4 MB
平均 146 ms

デフォルト値のみの設定よりもパフォーマンスが良くなってます。
デフォルト値を使用する際は型宣言をしたほうが良さそうですね。

今回は漠然と「そうなんだろうなぁ」って思ってたことを実際にベンチを取ってみたって感じです。
ちなみに
declare(strict_types = 1);
を記述したパターンも試してみたのですが、こちらは重くなりました。(チェック処理が入るからかな?って思ってます)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?