Created
July 4, 2011 22:10
-
-
Save asmuth/1064005 to your computer and use it in GitHub Desktop.
json<->marshal loading in ruby1.8<->ruby1.9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
MY_JSON_FILE = File.open('big_json.json').read | |
MY_MARSHAL_FILE = File.open('big_json.dat').read | |
class FileLoader | |
def load_via_json | |
JSON.parse(MY_JSON_FILE) | |
end | |
def load_via_marshal | |
Marshal.load(MY_MARSHAL_FILE) | |
end | |
end | |
def experiment(object, method, times) | |
puts "calling #{method} x #{times} times..." | |
start = Time.now.to_f | |
times.times{ object.send(method) } | |
finish = Time.now.to_f | |
duration = finish-start | |
puts "foobar: %.2fs (%.2f/s) [%.6f]" % [ | |
duration, # time taken for n loads | |
(times / duration), # loads per second | |
(duration / times) # time per load | |
] | |
duration / times | |
end | |
results = Hash.new | |
5.times do | |
%w(load_via_json load_via_marshal).each do |m| | |
result = experiment(FileLoader.new, m.intern, 1000) | |
results[m] ||= Array.new | |
results[m] << result | |
end | |
end | |
puts nil | |
results.each do |k, r| | |
avg = r.inject(0){ |memo, v| memo += v } / r.length | |
puts "avg: %.6fs | %s" % [avg, k] | |
end | |
# /home/paul/testfoo> ll big_json* | |
# -rw-r--r-- 1 paul paul 335K 2011-07-05 00:09 big_json.dat | |
# -rw-r--r-- 1 paul paul 330K 2011-07-05 00:10 big_json.json | |
# http://falbala.23loc.com/~paul/big_json.json | |
# /home/paul/testfoo> ruby1.9 bench.rb | |
# calling load_via_json x 1000 times... | |
# foobar: 3.00s (333.63/s) [0.002997] | |
# calling load_via_marshal x 1000 times... | |
# foobar: 3.09s (323.26/s) [0.003093] | |
# calling load_via_json x 1000 times... | |
# foobar: 2.96s (337.93/s) [0.002959] | |
# calling load_via_marshal x 1000 times... | |
# foobar: 3.10s (322.45/s) [0.003101] | |
# calling load_via_json x 1000 times... | |
# foobar: 2.96s (337.70/s) [0.002961] | |
# calling load_via_marshal x 1000 times... | |
# foobar: 3.09s (323.62/s) [0.003090] | |
# calling load_via_json x 1000 times... | |
# foobar: 2.96s (338.26/s) [0.002956] | |
# calling load_via_marshal x 1000 times... | |
# foobar: 3.10s (322.53/s) [0.003100] | |
# calling load_via_json x 1000 times... | |
# foobar: 2.97s (336.92/s) [0.002968] | |
# calling load_via_marshal x 1000 times... | |
# foobar: 3.09s (323.23/s) [0.003094] | |
# | |
# avg: 0.002968s | load_via_json | |
# avg: 0.003096s | load_via_marshal | |
# /home/paul/testfoo> ruby1.8 bench.rb | |
# calling load_via_json x 1000 times... | |
# foobar: 3.26s (307.18/s) [0.003255] | |
# calling load_via_marshal x 1000 times... | |
# foobar: 2.46s (405.68/s) [0.002465] | |
# calling load_via_json x 1000 times... | |
# foobar: 2.71s (368.53/s) [0.002713] | |
# calling load_via_marshal x 1000 times... | |
# foobar: 2.47s (404.14/s) [0.002474] | |
# calling load_via_json x 1000 times... | |
# foobar: 2.73s (366.47/s) [0.002729] | |
# calling load_via_marshal x 1000 times... | |
# foobar: 2.46s (406.39/s) [0.002461] | |
# calling load_via_json x 1000 times... | |
# foobar: 2.73s (366.52/s) [0.002728] | |
# calling load_via_marshal x 1000 times... | |
# foobar: 2.46s (405.74/s) [0.002465] | |
# calling load_via_json x 1000 times... | |
# foobar: 2.74s (364.92/s) [0.002740] | |
# calling load_via_marshal x 1000 times... | |
# foobar: 2.49s (401.58/s) [0.002490] | |
# nil | |
# avg: 0.002471s | load_via_marshal | |
# avg: 0.002833s | load_via_json | |
# /home/paul/testfoo> ruby1.8 -v | |
# ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux] | |
# /home/paul/testfoo> ruby1.9 -v | |
# ruby 1.9.0 (2008-06-20 revision 17482) [i486-linux] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment