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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
| require "benchmark"
include Benchmark
Benchmark.bm(21, "Total:") do|b|
n=100_000
puts "Create 100.000:"
t1 = b.report("%w()") do
n.times { %w(a b c d f e g h j k i l m n o p q r s t u v w y z 0 1 2 3 4 5 6 7 8 9) }
end
t1 += b.report("%w''") do
n.times { %w"a b c d f e g h j k i l m n o p q r s t u v w y z 0 1 2 3 4 5 6 7 8 9" }
end
t1 += b.report("split") do
n.times { "a b c d f e g h j k i l m n o p q r s t u v w y z 0 1 2 3 4 5 6 7 8 9".split(' ') }
end
t1 += b.report("[str]") do
n.times { ["a","b","c","d","f","e","g","h","j","k","i","l","m","n","o","p","q","r","s","t","u","v","w","y","z","0","1","2","3","4","5","6","7","8","9"] }
end
t1 += b.report("[num]") do
n.times { [0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4] }
end
t1 += b.report("Array.new(str)") do
n.times { Array.new(35, "a") }
end
t1 += b.report("Array.new(num)") do
n.times { Array.new(35, 0) }
end
t1 += b.report("Array.new(hash)") do
n.times { Array.new(35, {}) }
end
n=100_000
puts "Add 100.000:"
t2 = b.report("+= ['.']") do
a = []
n.times { a += ["."] }
end
t2 += b.report("+= [0]") do
a = []
n.times { a += [0] }
end
t2 += b.report("<< ['.']") do
a = []
n.times { a << ["."] }
end
t2 += b.report("<< [0]") do
a = []
n.times { a << [0] }
end
t2 += b.report("a = a+['.']") do
a = []
n.times { a = a + ["."] }
end
t2 += b.report("a = a+[0]") do
a = []
n.times { a = a + [0] }
end
n=100_000
puts "Concatenate 100.000:"
t3 = b.report("[num]+") do
n.times { [0,1,2,3,4]+[5,6,7,8,9]+[0,1,2,3,4,5,6,7,8,9] }
end
t3 += b.report("[str]+") do
n.times { ["a","b","c","d","f"]+["e","g","h","j","k"]+["i","l","m","n","o","p","q","r","s","t"] }
end
t3 += b.report("[mix]+") do
n.times { ["a","b","c","d","f"]+[5,6,7,8,9]+["i",0,"m",1,"o",2,"q",3,"s",4] }
end
t3 += b.report("[num]<<") do
n.times { [0,1,2,3,4]<<[5,6,7,8,9]<<[0,1,2,3,4,5,6,7,8,9] }
end
t3 += b.report("[str]<<") do
n.times { ["a","b","c","d","f"]<<["e","g","h","j","k"]<<["i","l","m","n","o","p","q","r","s","t"] }
end
t3 += b.report("[mix]<<") do
n.times { ["a","b","c","d","f"]<<[5,6,7,8,9]<<["i",0,"m",1,"o",2,"q",3,"s",4] }
end
t3 += b.report("[num].concat") do
n.times { [0,1,2,3,4].concat([5,6,7,8,9]).concat([0,1,2,3,4,5,6,7,8,9]) }
end
t3 += b.report("[str].concat") do
n.times { ["a","b","c","d","f"].concat(["e","g","h","j","k"]).concat(["i","l","m","n","o","p","q","r","s","t"]) }
end
t3 += b.report("[mix].concat") do
n.times { ["a","b","c","d","f"].concat([5,6,7,8,9]).concat(["i",0,"m",1,"o",2,"q",3,"s",4])}
end
t3 += b.report("union [num]") do
n.times { [0,14,2,3,4,53,682] | [3,4,53,6,7,84,9] }
end
t3 += b.report("union [str]") do
n.times { ["a","b","c","d","f"] | ["b","d","h","j","k"] }
end
a=Array.new(100, 0)
n=1_000_000
puts "Read 1.000.000:"
t4 = b.report("each:") do
n.times { a.each{|x| x} }
end
t4 += b.report("map:") do
n.times { a.map{|x| x} }
end
puts "Other 10.000:"
n=10_000
a=["a",nil,0,"b",1,nil] * 500
t5 = b.report(".compact:") do
n.times { a.compact }
end
t5 += b.report(".delete(nil):") do
n.times { a.delete(nil)}
end
a=[]
n.times {|x| a << x }
t5 += b.report("delete:") do
n.times {|x| a.delete(x) }
end
puts "-"*20
[t1+t2+t3+t4+t5]
end |