Skip to content

Instantly share code, notes, and snippets.

@mojombo
Created October 25, 2009 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mojombo/217853 to your computer and use it in GitHub Desktop.
Save mojombo/217853 to your computer and use it in GitHub Desktop.
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'rubygems'
require 'bert'
require 'json'
require 'yajl'
require 'benchmark'
ITER = 1_000
tiny = t[:ok, :awesome]
small = t[:ok, :answers, [42] * 42]
large = ["abc" * 1000] * 100
complex = [42, {:foo => 'bac' * 100}, t[(1..100).to_a]] * 10
tiny_encoded_bert = BERT.encode(tiny)
small_encoded_bert = BERT.encode(small)
large_encoded_bert = BERT.encode(large)
complex_encoded_bert = BERT.encode(complex)
tiny_encoded_json = JSON.dump(tiny)
small_encoded_json = JSON.dump(small)
large_encoded_json = JSON.dump(large)
complex_encoded_json = JSON.dump(complex)
tiny_encoded_yajl = Yajl::Encoder.encode(tiny)
small_encoded_yajl = Yajl::Encoder.encode(small)
large_encoded_yajl = Yajl::Encoder.encode(large)
complex_encoded_yajl = Yajl::Encoder.encode(complex)
tiny_encoded_ruby = Marshal.dump(tiny)
small_encoded_ruby = Marshal.dump(small)
large_encoded_ruby = Marshal.dump(large)
complex_encoded_ruby = Marshal.dump(complex)
Benchmark.bm(13) do |bench|
bench.report("BERT tiny") {ITER.times {BERT.decode(tiny_encoded_bert)}}
bench.report("BERT small") {ITER.times {BERT.decode(small_encoded_bert)}}
bench.report("BERT large") {ITER.times {BERT.decode(large_encoded_bert)}}
bench.report("BERT complex") {ITER.times {BERT.decode(complex_encoded_bert)}}
puts
bench.report("JSON tiny") {ITER.times {JSON.load(tiny_encoded_json)}}
bench.report("JSON small") {ITER.times {JSON.load(small_encoded_json)}}
bench.report("JSON large") {ITER.times {JSON.load(large_encoded_json)}}
bench.report("JSON complex") {ITER.times {JSON.load(complex_encoded_json)}}
puts
bench.report("YAJL tiny") {ITER.times {Yajl::Parser.parse(tiny_encoded_yajl)}}
bench.report("YAJL small") {ITER.times {Yajl::Parser.parse(small_encoded_yajl)}}
bench.report("YAJL large") {ITER.times {Yajl::Parser.parse(large_encoded_yajl)}}
bench.report("YAJL complex") {ITER.times {Yajl::Parser.parse(complex_encoded_yajl)}}
puts
bench.report("Ruby tiny") {ITER.times {Marshal.load(tiny_encoded_ruby)}}
bench.report("Ruby small") {ITER.times {Marshal.load(small_encoded_ruby)}}
bench.report("Ruby large") {ITER.times {Marshal.load(large_encoded_ruby)}}
bench.report("Ruby complex") {ITER.times {Marshal.load(complex_encoded_ruby)}}
end
user system total real
BERT tiny 0.000000 0.000000 0.000000 ( 0.001705)
BERT small 0.010000 0.000000 0.010000 ( 0.003673)
BERT large 0.430000 0.010000 0.440000 ( 0.502949)
BERT complex 0.090000 0.010000 0.100000 ( 0.090248)
JSON tiny 0.000000 0.000000 0.000000 ( 0.004300)
JSON small 0.020000 0.000000 0.020000 ( 0.016503)
JSON large 1.420000 0.000000 1.420000 ( 1.461110)
JSON complex 0.510000 0.010000 0.520000 ( 0.516117)
YAJL tiny 0.000000 0.000000 0.000000 ( 0.003885)
YAJL small 0.020000 0.000000 0.020000 ( 0.019154)
YAJL large 1.840000 0.010000 1.850000 ( 2.033403)
YAJL complex 0.560000 0.000000 0.560000 ( 0.572699)
Ruby tiny 0.010000 0.000000 0.010000 ( 0.002859)
Ruby small 0.000000 0.000000 0.000000 ( 0.005744)
Ruby large 0.010000 0.000000 0.010000 ( 0.010652)
Ruby complex 0.010000 0.000000 0.010000 ( 0.011968)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment