Skip to content

Instantly share code, notes, and snippets.

@nickmartini
Created July 13, 2009 17:20
Show Gist options
  • Save nickmartini/146300 to your computer and use it in GitHub Desktop.
Save nickmartini/146300 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'rubygems'
require 'couchrest'
SERVER = CouchRest.new
SERVER.default_database = 'misanthropy'
class Twat < CouchRest::ExtendedDocument
use_database SERVER.default_database
property :user_id
property :body
timestamps!
end
n = 5000
Benchmark.bm do |b|
b.report("for:") {
for i in 1..n do
t = Twat.new
t.user_id = i
t.body = "#{i} for dongs"
t.save
end
}
b.report("upto:") {
1.upto(n) do |i|
t = Twat.new
t.user_id = i
t.body = "#{i} upto dongs"
t.save
end
}
b.report("times:") {
n.times do |i|
t = Twat.new
t.user_id = i
t.body = "#{i} times dongs"
t.save
end
}
end
nmartini@athena:~$ ruby test.rb
user system total real
for: 6.770000 1.340000 8.110000 ( 66.657872)
upto: 6.310000 1.130000 7.440000 ( 65.514294)
times: 5.690000 1.220000 6.910000 ( 57.944218)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment