Load ids from a text file

file = File.open("list_of_ids.txt")
object_ids = file.readlines.map(&:chomp)

objects = Object.where(:id.in => object_ids).to_a

Write list of hashes to a CSV file

CSV.open("objects.csv", "wb") { |csv| 
  csv << objects[0].keys
  objects.each{|o| csv << o.values}
}

Benchmark code

Benchmark.measure { some_expensive_operation() }