Skip to content

Instantly share code, notes, and snippets.

@nmerouze
Created September 21, 2009 10:29
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 nmerouze/190180 to your computer and use it in GitHub Desktop.
Save nmerouze/190180 to your computer and use it in GitHub Desktop.
# MongoDB Java driver vs Ruby Driver with JRuby 1.3.1
#
# user system total real
# java driver (find) 2.266000 0.000000 2.266000 ( 2.266000)
# ruby driver (find) 6.790000 0.000000 6.790000 ( 6.790000)
# java driver (create) 0.602000 0.000000 0.602000 ( 0.602000)
# ruby driver (create) 3.963000 0.000000 3.963000 ( 3.963000)
require "java"
require "benchmark"
require "rubygems"
require "mongo"
require "mongo.jar"
class MongoJava
import "com.mongodb.Mongo"
import "com.mongodb.DBCollection"
import "com.mongodb.BasicDBObject"
import "com.mongodb.DBObject"
def initialize
@db = Mongo.new("mongojavatest")
@coll = @db.get_collection("testCollection")
@coll.drop
doc = BasicDBObject.new
doc.put "name", "MongoDB"
@coll.insert(doc)
end
def create
doc = BasicDBObject.new
doc.put "name", "MongoDB"
@coll.insert(doc)
end
def find
@coll.find_one
end
end
class MongoRuby
def initialize
@db = Mongo::Connection.new.db("mongorubytest")
@coll = @db.collection("testCollection")
@coll.insert({ "name" => "MongoDB" })
end
def create
@coll.insert({ "name" => "MongoDB" })
end
def find
@coll.find_one
end
end
jdriver = MongoJava.new
rdriver = MongoRuby.new
TIMES = 10_000
Benchmark.bm do |x|
x.report("java driver (find)") { TIMES.times { |n| jdriver.find } }
x.report("ruby driver (find)") { TIMES.times { |n| rdriver.find } }
x.report("java driver (create)") { TIMES.times { |n| jdriver.create } }
x.report("ruby driver (create)") { TIMES.times { |n| rdriver.create } }
end
# user system total real
# ruby 1.8.6 4.570000 0.400000 4.970000 ( 6.119891)
#
# user system total real
# ruby 1.9.1 2.740000 0.460000 3.200000 ( 4.251336)
require "benchmark"
require "rubygems"
require "mongo"
class MongoRuby
def initialize
@db = Mongo::Connection.new.db("mongorubytest")
@coll = @db.collection("testCollection")
@coll.insert({ "name" => "MongoDB" })
end
def find
@coll.find_one
end
end
rdriver = MongoRuby.new
TIMES = 10_000
Benchmark.bm do |x|
x.report("ruby driver") { TIMES.times { |n| rdriver.find } }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment