Skip to content

Instantly share code, notes, and snippets.

@jlindley
Created August 9, 2008 15:16
Show Gist options
  • Save jlindley/4683 to your computer and use it in GitHub Desktop.
Save jlindley/4683 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.050000 0.000000 0.050000 ( 0.048411)
# string marshalling 0.580000 0.020000 0.600000 ( 0.659637)
#
# object saved in hash 0.040000 0.000000 0.040000 ( 0.047936)
# object marshalling 9.240000 0.170000 9.410000 ( 9.932777)
#
# string load from hash 0.040000 0.000000 0.040000 ( 0.044545)
# string marshal load 0.440000 0.010000 0.450000 ( 0.478145)
#
# object load from hash 0.040000 0.000000 0.040000 ( 0.041927)
# object marshal load 4.360000 0.090000 4.450000 ( 4.689497)
# object db reload 43.910000 3.520000 47.430000 ( 75.749377)
require 'benchmark'
namespace :bm do
desc "Benchmark hash / marshalling"
task :marshal => :environment do
n = 100000
s = "Hello, world!"
o = Student.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
x.report('object db reload'.rjust(24)) do
n.times do
obj = Student.find(o)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment