LoginSignup
55
57

More than 5 years have passed since last update.

PHPでファイル名文字列から拡張子のみ取得する時に最も速いのはどの方法か

Last updated at Posted at 2016-01-12

ちょっとしたことだけど塵も積もれば速くなるので悩まないためにメモ。

方法1

なんとなく速そうなやつ。

$ext = substr($filename, strrpos($filename, '.') + 1);

方法2

記述が簡単なやつ。

$ext = pathinfo($filename, PATHINFO_EXTENSION);

方法3

ドットで配列にして最後を取得。
ワンライナーでできないのが難。

$arr = explode('.', $filename);
$ext = end($arr);

方法4

方法3と同じだけど array_pop を使用。

$arr = explode('.', $filename);
$ext = array_pop($arr);

結果

各10000回実行した結果。

方法 マイクロ秒
方法1(substr) :thumbsup: 0.0030159950256348
方法2(pathinfo) 0.0118758678436280
方法3(explode + end) 0.0055711269378662
方法4(explode + array_pop) 0.0053489208221436

やっぱり文字列系が速いらしい。

55
57
1

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
55
57