-
Notifications
You must be signed in to change notification settings - Fork 29
/
benchmark.rb
145 lines (122 loc) · 3.23 KB
/
benchmark.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
$:.unshift(File.expand_path('lib'))
require 'benchmark'
require 'time'
require 'timeliness'
if defined?(JRUBY_VERSION)
# Warm up JRuby
20_000.times do
Time.parse("2000-01-04 12:12:12")
Timeliness::Parser.parse("2000-01-04 12:12:12", :datetime)
end
end
n = 10_000
Benchmark.bm(40) do |x|
x.report('timeliness - datetime') {
n.times do
Timeliness::Parser.parse("2000-01-04 12:12:12", :datetime)
end
}
x.report('timeliness - datetime with :format') {
n.times do
Timeliness::Parser.parse("2000-01-04 12:12:12", :datetime, format: 'yyyy-mm-dd hh:nn:ss')
end
}
x.report('timeliness - date') {
n.times do
Timeliness::Parser.parse("2000-01-04", :date)
end
}
x.report('timeliness - date as datetime') {
n.times do
Timeliness::Parser.parse("2000-01-04", :datetime)
end
}
x.report('timeliness - time') {
n.times do
Timeliness::Parser.parse("12:01:02", :time)
end
}
x.report('timeliness - no type with datetime value') {
n.times do
Timeliness::Parser.parse("2000-01-04 12:12:12")
end
}
x.report('timeliness - no type with date value') {
n.times do
Timeliness::Parser.parse("2000-01-04")
end
}
x.report('timeliness - no type with time value') {
n.times do
Timeliness::Parser.parse("12:01:02")
end
}
x.report('timeliness - invalid format datetime') {
n.times do
Timeliness::Parser.parse("20xx-01-04 12:12:12", :datetime)
end
}
x.report('timeliness - invalid format date') {
n.times do
Timeliness::Parser.parse("20xx-01-04", :date)
end
}
x.report('timeliness - invalid format time') {
n.times do
Timeliness::Parser.parse("12:xx:02", :time)
end
}
x.report('timeliness - invalid value datetime') {
n.times do
Timeliness::Parser.parse("2000-01-32 12:12:12", :datetime)
end
}
x.report('timeliness - invalid value date') {
n.times do
Timeliness::Parser.parse("2000-01-32", :date)
end
}
x.report('timeliness - invalid value time') {
n.times do
Timeliness::Parser.parse("12:61:02", :time)
end
}
x.report('ISO regexp for datetime') {
n.times do
"2000-01-04 12:12:12" =~ /\A(\d{4})-(\d{2})-(\d{2}) (\d{2})[\. :](\d{2})([\. :](\d{2}))?\Z/
Time.mktime($1.to_i, $2.to_i, $3.to_i, $3.to_i, $5.to_i, $6.to_i)
end
}
x.report('Time.parse - valid') {
n.times do
Time.parse("2000-01-04 12:12:12")
end
}
x.report('Time.parse - invalid ') {
n.times do
Time.parse("2000-01-32 12:12:12") rescue nil
end
}
x.report('Date._parse - valid') {
n.times do
hash = Date._parse("2000-01-04 12:12:12")
Time.mktime(hash[:year], hash[:mon], hash[:mday], hash[:hour], hash[:min], hash[:sec])
end
}
x.report('Date._parse - invalid ') {
n.times do
hash = Date._parse("2000-01-32 12:12:12")
Time.mktime(hash[:year], hash[:mon], hash[:mday], hash[:hour], hash[:min], hash[:sex]) rescue nil
end
}
x.report('strptime - valid') {
n.times do
DateTime.strptime("2000-01-04 12:12:12", '%Y-%m-%d %H:%M:%s')
end
}
x.report('strptime - invalid') {
n.times do
DateTime.strptime("2000-00-04 12:12:12", '%Y-%m-%d %H:%M:%s') rescue nil
end
}
end