forked from adzap/validates_timeliness
-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.rb
99 lines (83 loc) · 1.89 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
$:.unshift(File.expand_path('lib'))
require 'date'
require 'parsedate'
require 'benchmark'
require 'rubygems'
require 'active_record'
require 'validates_timeliness'
def parse(*args)
ValidatesTimeliness::Parser.parse(*args)
end
n = 10000
Benchmark.bm do |x|
x.report('timeliness - datetime') {
n.times do
parse("2000-01-04 12:12:12", :datetime)
end
}
x.report('timeliness - date') {
n.times do
parse("2000-01-04", :date)
end
}
x.report('timeliness - date as datetime') {
n.times do
parse("2000-01-04", :datetime)
end
}
x.report('timeliness - time') {
n.times do
parse("12:01:02", :time)
end
}
x.report('timeliness - invalid format datetime') {
n.times do
parse("20xx-01-04 12:12:12", :datetime)
end
}
x.report('timeliness - invalid format date') {
n.times do
parse("20xx-01-04", :date)
end
}
x.report('timeliness - invalid format time') {
n.times do
parse("12:xx:02", :time)
end
}
x.report('timeliness - invalid value datetime') {
n.times do
parse("2000-01-32 12:12:12", :datetime)
end
}
x.report('timeliness - invalid value date') {
n.times do
parse("2000-01-32", :date)
end
}
x.report('timeliness - invalid value time') {
n.times do
parse("12:61:02", :time)
end
}
x.report('date/time') {
n.times do
"2000-01-04 12:12:12" =~ /\A(\d{4})-(\d{2})-(\d{2}) (\d{2})[\. :](\d{2})([\. :](\d{2}))?\Z/
arr = [$1, $2, $3, $3, $5, $6].map {|i| i.to_i }
Date.new(*arr[0..2])
Time.mktime(*arr)
end
}
x.report('parsedate') {
n.times do
arr = ParseDate.parsedate("2000-01-04 12:12:12")
Date.new(*arr[0..2])
Time.mktime(*arr)
end
}
x.report('strptime') {
n.times do
DateTime.strptime("2000-01-04 12:12:12", '%Y-%m-%d %H:%M:%s')
end
}
end