LoginSignup
1
1

More than 3 years have passed since last update.

pluckよりもmapのほうが高速なケース⇦今もそうなのか?

Last updated at Posted at 2019-06-04

以下の記事(pluckよりもmapのほうが高速なケース)を見つけ、Railsのバーションアップがなされ今はどうなのか気になったので再計測しました。
-https://qiita.com/metheglin/items/18064851a8f00dab67f8

結論から言うと、結果は以前の記事の結果と同じ感じでした。

pluckを使うべきケース

Profile.all.pluck(:id)
  # => SELECT "profiles"."id" FROM "profiles"
Profile.all.map(&:id)
  # => SELECT "profiles".* FROM "profiles"

Benchmark

n = 2000
Benchmark.bm do |x|
  x.report { n.times { Profile.all.pluck(:id) } }
  x.report { n.times { Profile.all.map(&:id) } }
end
user system total real
pluck(:id) 5.874142 1.085963 6.960105 8.259476
map(&:id) 60.618729 1.614024 62.232753 65.682491

mapを使うべきケース

Benchmark

profiles = Profile.all
n = 2000
Benchmark.bm do |x|
  x.report { n.times { profiles.pluck(:id) } }
  x.report { n.times { profiles.map(&:id) } }
end
user system total real
pluck(:id) 5.392492 0.977601 6.370093 7.496229
map(&:id) 1.595164 0.016207 1.611371 1.621283

Environment

※OSはMac OSX ver.10.14.5
※CPU2.3GHz Intel Corei5 / メモリ8G
※DBはMySQL
※Railsバージョン "5.0.7.2"

1
1
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
1
1