nmerouze (owner)

Revisions

gist: 190180 Download_button fork
public
Public Clone URL: git://gist.github.com/190180.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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