@@ -53,16 +53,25 @@ case class DatesBench() extends SimpleScalaBenchmark {
53
53
def time_hand_less_bad (n : Int ) =
54
54
hand_less_alloc(n, " 201x-01-01" )
55
55
56
+ def time_actual_ok (n : Int ) =
57
+ actual(n, " 2012-01-01" )
58
+
59
+ def time_actual_invalid (n : Int ) =
60
+ actual(n, " 2012-99-01" )
61
+
62
+ def time_actual_bad (n : Int ) =
63
+ actual(n, " 201x-01-01" )
64
+
56
65
/*
57
66
* The basic joda approach, note: parseLocalDate throws exceptions so the catch is required.
58
67
*/
59
68
def joda (n : Int , s : String ) = {
60
69
val f = DateTimeFormat .forPattern(" yyyy-MM-dd" )
61
- repeat[String \/ Date ](n) {
70
+ repeat[Option [ Date ] ](n) {
62
71
try {
63
72
val d = f.parseLocalDate(s)
64
- Date .unsafe(d.getYear.toShort, d.getMonthOfYear.toByte, d.getDayOfMonth.toByte).right[ String ]
65
- } catch { case e : Throwable => " bad " .left }
73
+ Date .unsafe(d.getYear.toShort, d.getMonthOfYear.toByte, d.getDayOfMonth.toByte).some
74
+ } catch { case e : Throwable => None }
66
75
}
67
76
}
68
77
@@ -71,12 +80,12 @@ case class DatesBench() extends SimpleScalaBenchmark {
71
80
*/
72
81
def regex (n : Int , s : String ) = {
73
82
val DateParser = """ (\d\d\d\d)-(\d\d)-(\d\d)""" .r
74
- repeat[String \/ Date ](n) {
83
+ repeat[Option [ Date ] ](n) {
75
84
s match {
76
85
case DateParser (y, m, d) =>
77
- try Date .create(y.toShort, m.toByte, d.toByte).get.right
78
- catch { case e : Throwable => " bad " .left }
79
- case _ => " bad " .left
86
+ try Date .create(y.toShort, m.toByte, d.toByte)
87
+ catch { case e : Throwable => None }
88
+ case _ => None
80
89
}
81
90
}
82
91
}
@@ -85,26 +94,34 @@ case class DatesBench() extends SimpleScalaBenchmark {
85
94
* A crude parser that unpacks things by hand.
86
95
*/
87
96
def hand (n : Int , s : String ) =
88
- repeat[String \/ Date ](n) {
97
+ repeat[Option [ Date ] ](n) {
89
98
if (s.length != 10 || s.charAt(4 ) != '-' || s.charAt(7 ) != '-' )
90
- " bad " .left
99
+ None
91
100
else try
92
- Date .create(s.substring(0 , 4 ).toShort, s.substring(5 , 7 ).toByte, s.substring(9 , 10 ).toByte).get.right
93
- catch { case e : Throwable => " bad " .left }
101
+ Date .create(s.substring(0 , 4 ).toShort, s.substring(5 , 7 ).toByte, s.substring(8 , 10 ).toByte)
102
+ catch { case e : Throwable => None }
94
103
}
95
104
96
105
/*
97
106
* A crude parser that unpacks things by hand with less allocation
98
107
*/
99
108
def hand_less_alloc (n : Int , s : String ) =
100
- repeat[String \/ Date ](n) {
109
+ repeat[Option [ Date ] ](n) {
101
110
if (s.length != 10 || s.charAt(4 ) != '-' || s.charAt(7 ) != '-' )
102
- " bad " .left
111
+ None
103
112
else try {
104
113
val y = s.substring(0 , 4 ).toShort
105
114
val m = s.substring(5 , 7 ).toByte
106
- val d = s.substring(9 , 10 ).toByte
107
- if (Date .isValid(y, m, d)) Date .unsafe(y, m, d).right else " bad" .left
108
- } catch { case e : Throwable => " bad" .left }
115
+ val d = s.substring(8 , 10 ).toByte
116
+ if (Date .isValid(y, m, d)) Date .unsafe(y, m, d).some else None
117
+ } catch { case e : Throwable => None }
118
+ }
119
+
120
+ /*
121
+ * A crude parser that unpacks things by hand with less allocation
122
+ */
123
+ def actual (n : Int , s : String ) =
124
+ repeat[Option [Date ]](n) {
125
+ Dates .date(s)
109
126
}
110
127
}
0 commit comments