Skip to content

Instantly share code, notes, and snippets.

@ryanb
Forked from jlindley/gist:4683
Created August 9, 2008 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanb/4688 to your computer and use it in GitHub Desktop.
Save ryanb/4688 to your computer and use it in GitHub Desktop.
# AR Object dup vs marshal benchmarks
#
# re: http://rails.lighthouseapp.com/projects/8994/tickets/785
#
# user system total real
# string saved in hash 0.060000 0.000000 0.060000 ( 0.051861)
# string marshalling 0.630000 0.000000 0.630000 ( 0.643304)
#
# object saved in hash 0.060000 0.000000 0.060000 ( 0.052789)
# object marshalling 7.020000 0.030000 7.050000 ( 7.073463)
#
# string load from hash 0.040000 0.000000 0.040000 ( 0.047476)
# string marshal load 0.450000 0.000000 0.450000 ( 0.447863)
#
# object load from hash 0.040000 0.000000 0.040000 ( 0.045871)
# object marshal load 3.550000 0.010000 3.560000 ( 3.575256)
# object db reload 77.960000 3.300000 81.260000 ( 82.124798)
require 'benchmark'
namespace :bm do
desc "Benchmark hash / marshalling"
task :marshal => :environment do
n = 100000
s = "Hello, world!"
o = Episode.first
h = Hash.new
s_marshalled = Marshal.dump(s)
o_marshalled = Marshal.dump(o)
h_loaded = {:str => s, :obj => o}
h_marshalled = {:str => s_marshalled, :obj => o_marshalled}
Benchmark.bm do |x|
x.report('string saved in hash'.rjust(24)) do
n.times do
h[:key] = s
end
end
x.report('string marshalling'.rjust(24)) do
n.times do
h[:key] = Marshal.dump(s)
end
end
puts
x.report('object saved in hash'.rjust(24)) do
n.times do
h[:key] = o
end
end
x.report('object marshalling'.rjust(24)) do
n.times do
h[:key] = Marshal.dump(o)
end
end
puts
x.report('string load from hash'.rjust(24)) do
n.times do
str = h_loaded[:str]
end
end
x.report('string marshal load'.rjust(24)) do
n.times do
str = Marshal.load(h_marshalled[:str])
end
end
puts
x.report('object load from hash'.rjust(24)) do
n.times do
obj = h_loaded[:obj]
end
end
x.report('object marshal load'.rjust(24)) do
n.times do
obj = Marshal.load(h_marshalled[:obj])
end
end
Episode.uncached do
x.report('object db reload'.rjust(24)) do
n.times do
obj = Episode.find(o)
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment