Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

方法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

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした