ryanb (owner)

Fork Of

Revisions

  • 35a5ad ryanb Sat Aug 09 10:00:05 -0700 2008
  • 12a485 ryanb Sat Aug 09 09:57:02 -0700 2008
  • a78064 jlindley Sat Aug 09 09:19:02 -0700 2008
  • d9b877 jlindley Sat Aug 09 08:16:15 -0700 2008
gist: 4688 Download_button fork
public
Public Clone URL: git://gist.github.com/4688.git
Text
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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