pauldix (owner)

Forks

Revisions

gist: 7549 Download_button fork
public
Public Clone URL: git://gist.github.com/7549.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
require 'benchmark'
require 'rubygems'
require 'json'
require 'yaml'
include Benchmark
 
benchmark_iterations = 1
large_single_dimension_array = [42, 123.123] * 5000
large_single_dimension_hash = {}
10000.times do |i|
  large_single_dimension_hash["key#{i}".intern] = i
end
 
class Fixnum
  alias to_ruby to_s
end
 
class Float
  alias to_ruby to_s
end
 
class String
  alias to_ruby inspect
end
 
class Symbol
  def to_ruby
    ":#{to_s}"
  end
end
 
class Array
  def to_ruby
      "[#{map {|x| x.to_ruby}.join(',')}]"
  end
end
 
class Hash
  def to_ruby
    "{#{map {|k, v| "#{k.to_ruby} => #{v.to_ruby}"}.join(", ")}}"
  end
end
 
benchmark do |t|
  t.report("array marshal") do
    benchmark_iterations.times {
      Marshal.load(Marshal.dump(large_single_dimension_array))
    }
  end
  t.report("array json") do
    benchmark_iterations.times {
      JSON.load(JSON.dump(large_single_dimension_array))
    }
  end
  t.report("array eval") do
    benchmark_iterations.times {
      eval(large_single_dimension_array.to_ruby)
    }
  end
  t.report("array yaml") do
    benchmark_iterations.times {
      YAML.load(YAML.dump(large_single_dimension_array))
    }
  end
  t.report("hash marshal") do
    benchmark_iterations.times {
      Marshal.load(Marshal.dump(large_single_dimension_hash))
    }
  end
  t.report("hash json") do
    benchmark_iterations.times {
      JSON.load(JSON.dump(large_single_dimension_hash))
    }
  end
  t.report("hash eval") do
    benchmark_iterations.times {
      eval(large_single_dimension_hash.to_ruby)
    }
  end
  t.report("hash yaml") do
    benchmark_iterations.times {
      YAML.load(YAML.dump(large_single_dimension_hash))
    }
  end
end