I highly recommend you test this stuff out yourself, you can learn a lot! Commence Scala REPL:
scala> import scala.util.Try
import scala.util.Try
< import printTime function from our repo >
scala> val list = List("The", "first", "one", "is", "11", "the", "second", "is", "22")
list: List[String] = List(The, first, one, is, 11, the, second, is, 22)
scala> var x: List[Int] = Nil
x: List[Int] = List()
OK, the environment is set up. Here's your first function (Try):
scala> def f1(l: List[String], n: Int) = {
var i = 0
while (i < n) {
x = l.flatMap(s => Try(s.toInt).toOption)
i += 1
}
}
f1: (l: List[String], n: Int)Unit
The second function (regex):
scala> def f2(l: List[String], n: Int) = {
var i = 0
while (i < n) {
x = l.filter(_.matches("\\d+")).map(_.toInt)
i += 1
}
}
f2: (l: List[String], n: Int)Unit
Timings:
scala> printTime(f1(list, 100000)) // Try
time: 4.152s
scala> printTime(f2(list, 100000)) // regex
time: 565.107ms
Well, we've learned that handling exceptions inside a flatMap is a very inefficient way to do things. This is partly because exception handling produces bad assembly code, and partly because flatMaps with options do a lot of extra allocation and boxing. Regex is ~8x faster! But...is regex fast?
scala> def f3(l: List[String], n: Int) = {
var i = 0
while (i < n) {
x = l.filter(_.forall(_.isDigit)).map(_.toInt)
i += 1
}
}
f3: (l: List[String], n: Int)Unit
scala> printTime(f3(list, 100000)) // isDigit
time: time: 70.960ms
Replacing regex with character isDigit calls gave us another order of magnitude improvement. The lesson here is to avoid try/catch handling at all costs, avoid using regex whenever possible, and don't be afraid to write performance comparisons!
val Reg = "(\\d+)".rand b) doing thefilterand themapin one single step instead of two, e.g.ls.collect{ case Reg(n) => n.toInt }Trywhich is not good I think. The universal answer to my question IMHO should consider three different situations: if F is the possibility to meet number in the list than the first situation is F is almost 0, the second F = 0,5, and finally F is almost 1. BTW: I'm quite new to stack overflow, should I update my question with these details or comment is enough?