/
struct.rb
85 lines (71 loc) · 1.93 KB
/
struct.rb
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 File.expand_path('herbs_helper')
Contact = Struct.new(:first_name, :last_name, :home_number, :cell_number, :email )
m = 10_000
n = 100_000
o = 200_000
Benchmark.bm do |x|
x.bench("Struct.new\n", :macruby => false) do
m.times do |i|
Struct.new("Contact#{i}", :first_name, :last_name, :home_number, :cell_number, :email )
end
end
lrz = Contact.new('Laurent', 'Sansonetti', '123-456-5555', '555-444-3333', 'lrz@gmail.com')
matt = Contact.new('Matt', 'Aimonetti', '858-555-5555', '555-444-3333', 'mattaimonetti@gmail.com')
x.bench("==\n") do
n.times do
matt == lrz
end
end
x.bench("eql?\n") do
n.times do
matt.eql?(lrz)
end
end
x.bench("new instance\n") do
n.times do
Contact.new('Matt', 'Aimonetti', '858-555-5555', '555-444-3333', 'mattaimonetti@gmail.com')
end
end
x.bench("attribute reference by string\n") do
o.times{ matt['home_number'] }
end
x.bench("attribute reference by symbol\n") do
o.times{ matt[:home_number] }
end
x.bench("attribute reference by index\n") do
o.times{ matt[2] }
end
x.bench("each {|obj| block }\n") do
o.times{ matt.each{|x| x} }
end
x.bench("each_pair {|sym, obj| block }\n") do
o.times{ matt.each_pair{|name, value| name} }
end
x.bench("length\n") do
o.times{ matt.length }
end
x.bench("members\n") do
o.times{ matt.members }
end
x.bench("select{|i| block}\n") do
o.times{ matt.select{|i| true} }
end
x.bench("values_at(indexes)}\n") do
o.times{ matt.values_at(1,3) }
end
x.bench("values_at(negative indexes)}\n") do
o.times{ matt.values_at(-1,-3) }
end
x.bench("values_at(ranges)}\n") do
o.times{ matt.values_at(1..3, 2...5) }
end
x.bench("hash\n") do
n.times{ matt.hash }
end
x.bench("to_s\n") do
n.times{ matt.to_s }
end
x.bench("to_a\n") do
n.times{ matt.to_a }
end
end