Skip to content

Instantly share code, notes, and snippets.

@thr3a
Created December 3, 2015 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thr3a/0717a9d3a6d5c363fad2 to your computer and use it in GitHub Desktop.
Save thr3a/0717a9d3a6d5c363fad2 to your computer and use it in GitHub Desktop.
require 'benchmark'
namespace :bulk do
desc "TODO"
task insert: :environment do
Task.delete_all
Benchmark.bm 100 do |r|
r.report "normal" do
1000.times do |i|
Task.create(title: "#{i}", content: "content #{i}")
end
end
r.report "bulk" do
tasks = []
1000.times do |i|
tasks << Task.new(title: "#{i}", content: "content #{i}")
end
Task.import tasks
end
r.report "without validate" do
tasks = []
1000.times do |i|
tasks << Task.new(title: "#{i}", content: "content #{i}")
end
Task.import tasks, validate: false
end
r.report "without validate and new" do
tasks = []
1000.times do |i|
tasks << ["#{i}", "content #{i}"]
end
Task.import ['title', 'content'], tasks, validate: false
end
end
end
end
mPro:hoge thr3a$ rake bulk:insert
user system total real
normal 1.360000 0.470000 1.830000 ( 2.095945)
bulk 0.310000 0.010000 0.320000 ( 0.317949)
without validate 0.160000 0.000000 0.160000 ( 0.168920)
without validate and new 0.080000 0.000000 0.080000 ( 0.085750)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment