Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save oogali/908267 to your computer and use it in GitHub Desktop.
Save oogali/908267 to your computer and use it in GitHub Desktop.
The serialization showdown.
feed_hash = feed.to_hash
puts Benchmark.bm(20) { |x|
x.report('yaml') { (1..1000).each { feed_hash.to_yaml } }
x.report('yaml+zlib') { (1..1000).each { Zlib::Deflate.deflate(feed_hash.to_yaml) } }
x.report('json') { (1..1000).each { feed_hash.to_json } }
x.report('json+zlib') { (1..1000).each { Zlib::Deflate.deflate(feed_hash.to_json) } }
x.report('bson') { (1..1000).each { BSON.serialize feed_hash } }
x.report('marshal') { (1..1000).each { Marshal::dump(feed_hash) } }
x.report('marshal+zlib') { (1..1000).each { Zlib::Deflate.deflate(Marshal::dump(feed_hash)) } }
x.report('yajl') { (1..1000).each { Yajl::Encoder.encode(feed_hash) } }
x.report('yajl+zlib') { (1..1000).each { Zlib::Deflate.deflate(Yajl::Encoder.encode(feed_hash)) } }
}
$ uname -a
Darwin roadrunner.local 10.7.0 Darwin Kernel Version 10.7.0: Sat Jan 29 15:17:16 PST 2011; root:xnu-1504.9.37~1/RELEASE_I386 i386
$ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
$ gem query | egrep 'yaml|json|bson|yajl'
bson (1.3.0)
bson_ext (1.3.0)
json (1.4.2)
json_pure (1.4.3)
multi_json (0.0.5)
yajl-ruby (0.8.2)
Results on Ruby 1.8.7 (stock Snow Leopard version):
user system total real
yaml 10.300000 0.170000 10.470000 ( 10.450364)
yaml+zlib 11.230000 0.170000 11.400000 ( 11.566976)
json 0.390000 0.000000 0.390000 ( 0.391399)
json+zlib 1.010000 0.010000 1.020000 ( 1.015337)
bson 0.090000 0.000000 0.090000 ( 0.088657)
marshal 0.180000 0.000000 0.180000 ( 0.181146)
marshal+zlib 0.800000 0.000000 0.800000 ( 0.805794)
yajl 0.380000 0.000000 0.380000 ( 0.385006)
yajl+zlib 1.020000 0.010000 1.030000 ( 1.017877)
$ gem install bson bson_ext json yajl-ruby
$ gem query | egrep 'yaml|json|bson|yajl'
bson (1.3.0)
bson_ext (1.3.0)
json (1.5.1, 1.4.2)
json_pure (1.4.3)
multi_json (0.0.5)
yajl-ruby (0.8.2)
Results on Ruby 1.8.7 (stock Snow Leopard version, but with updated gems)
user system total real
yaml 10.320000 0.200000 10.520000 ( 10.586645)
yaml+zlib 10.840000 0.220000 11.060000 ( 11.079832)
json 0.380000 0.000000 0.380000 ( 0.381827)
json+zlib 1.040000 0.000000 1.040000 ( 1.039673)
bson 0.070000 0.010000 0.080000 ( 0.076006)
marshal 0.190000 0.000000 0.190000 ( 0.195457)
marshal+zlib 0.800000 0.000000 0.800000 ( 0.803859)
yajl 0.460000 0.000000 0.460000 ( 0.455251)
yajl+zlib 1.090000 0.010000 1.100000 ( 1.097557)
$ rvm use 1.9.2
Using ~/.rvm/gems/ruby-1.9.2-p180
$ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.6.0]
$ gem query | egrep 'yaml|json|bson|yajl'
bson (1.3.0)
bson_ext (1.3.0)
json (1.5.1)
yajl-ruby (0.8.2)
Results on Ruby 1.9.2 (patchlevel 180):
user system total real
yaml 7.140000 0.050000 7.190000 ( 7.376373)
yaml+zlib 7.120000 0.050000 7.170000 ( 7.287746)
json 0.360000 0.000000 0.360000 ( 0.365976)
json+zlib 0.500000 0.000000 0.500000 ( 0.502679)
bson 0.250000 0.000000 0.250000 ( 0.250349)
marshal 0.300000 0.000000 0.300000 ( 0.304102)
marshal+zlib 0.510000 0.000000 0.510000 ( 0.514293)
yajl 0.320000 0.010000 0.330000 ( 0.434203)
yajl+zlib 0.500000 0.000000 0.500000 ( 0.507787)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment