Skip to content

Instantly share code, notes, and snippets.

@nofxx
Created June 28, 2010 23:07
Show Gist options
  • Save nofxx/456519 to your computer and use it in GitHub Desktop.
Save nofxx/456519 to your computer and use it in GitHub Desktop.
JSON vs BSON
#!/usr/bin/env ruby
require 'benchmark'
require 'json'
require 'bson'
T = 100_000
[{ :foo => 2 }, { :foo => 2, :bar => 3, :baz => "long very very long and big and uncut string" },
{ :foo => 2, :bar => 3, :baz => "long very very long and big and uncut string",
:qux => "another big string passing very cool stuff from somewhere to nowhere",
:quux => "another big string passing very cool stuff from somewhere to nowhere",
:corge => "another big string passing very cool stuff from somewhere to nowhere",
:grault => "another big string passing very cool stuff from somewhere to nowhere"}].each do |hsh|
jsonhsh = JSON.dump(hsh)
bsonhsh = BSON.serialize(hsh)
puts
puts "Running #{T} times..."
puts hsh
puts
Benchmark.bmbm do |b|
b.report("JSON Dump") { T.times { JSON.dump(hsh) }}
b.report("JSON Load") { T.times { JSON.load(jsonhsh) }}
end
puts
Benchmark.bmbm do |b|
b.report("BSON Dump") { T.times { BSON.serialize(hsh) }}
b.report("BSON Load") { T.times { BSON.deserialize(bsonhsh) }}
end
end
__END__
Running 100000 times...
{:foo=>2}
Rehearsal ---------------------------------------------
JSON Dump 2.760000 0.020000 2.780000 ( 2.903587)
JSON Load 0.490000 0.010000 0.500000 ( 0.537410)
------------------------------------ total: 3.280000sec
user system total real
JSON Dump 2.770000 0.000000 2.770000 ( 2.905496)
JSON Load 0.470000 0.000000 0.470000 ( 0.498178)
Rehearsal ---------------------------------------------
BSON Dump 0.640000 0.010000 0.650000 ( 0.689085)
BSON Load 0.490000 0.000000 0.490000 ( 0.515012)
------------------------------------ total: 1.140000sec
user system total real
BSON Dump 0.680000 0.000000 0.680000 ( 0.718342)
BSON Load 0.480000 0.000000 0.480000 ( 0.475789)
Running 100000 times...
{:foo=>2, :bar=>3, :baz=>"long very very long and big and uncut string"}
Rehearsal ---------------------------------------------
JSON Dump 3.030000 0.010000 3.040000 ( 3.186499)
JSON Load 0.640000 0.000000 0.640000 ( 0.676488)
------------------------------------ total: 3.680000sec
user system total real
JSON Dump 3.010000 0.000000 3.010000 ( 3.115569)
JSON Load 0.640000 0.000000 0.640000 ( 0.674841)
Rehearsal ---------------------------------------------
BSON Dump 1.240000 0.010000 1.250000 ( 1.294717)
BSON Load 0.940000 0.000000 0.940000 ( 0.971074)
------------------------------------ total: 2.190000sec
user system total real
BSON Dump 1.240000 0.000000 1.240000 ( 1.276853)
BSON Load 0.940000 0.000000 0.940000 ( 0.989220)
Running 100000 times...
{:foo=>2, :bar=>3, :baz=>"long very very long and big and uncut string", :qux=>"another big string passing very cool stuff from somewhere to nowhere", :quux=>"another big string passing very cool stuff from somewhere to nowhere", :corge=>"another big string passing very cool stuff from somewhere to nowhere", :grault=>"another big string passing very cool stuff from somewhere to nowhere"}
Rehearsal ---------------------------------------------
JSON Dump 3.710000 0.000000 3.710000 ( 3.843567)
JSON Load 1.040000 0.000000 1.040000 ( 1.043548)
------------------------------------ total: 4.750000sec
user system total real
JSON Dump 3.750000 0.010000 3.760000 ( 3.788305)
JSON Load 1.040000 0.000000 1.040000 ( 1.059873)
Rehearsal ---------------------------------------------
BSON Dump 2.370000 0.000000 2.370000 ( 2.386694)
BSON Load 3.050000 0.000000 3.050000 ( 3.097794)
------------------------------------ total: 5.420000sec
user system total real
BSON Dump 2.400000 0.000000 2.400000 ( 2.413159)
BSON Load 3.010000 0.050000 3.060000 ( 3.079661)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment