Advertisement
Guest User

scala_split_code

a guest
Jul 28th, 2011
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. import scala.collection.mutable.ArrayBuffer
  2. import scala.annotation.tailrec
  3. import scala.collection.mutable.ListBuffer
  4. //import org.apache.commons.lang3.StringUtils
  5.  
  6.  
  7. object Test
  8. {
  9. def split(s: String, c: Char, i: Int = 0): List[String] = if (i < 0) Nil else {
  10. val p = s indexOf (c, i)
  11. if (p < 0) s.substring(i) :: Nil else s.substring(i, p) :: split(s, c, p + 1)
  12. }
  13.  
  14. def split2(s: String, c: Char): Seq[String] = {
  15. val buffer = ListBuffer.empty[String]
  16. @tailrec def recurse(i: Int): Seq[String] = {
  17. val p = s indexOf (c, i)
  18. if (p < 0) {
  19. buffer += s.substring(i)
  20. buffer.toList
  21. } else {
  22. buffer += s.substring(i, p)
  23. recurse(p + 1)
  24. }
  25. }
  26. recurse(0)
  27. }
  28.  
  29. def main(args: Array[String])
  30. {
  31. var len = 0
  32. val t1 = System.currentTimeMillis()
  33. io.Source.fromFile(args(0)).getLines.foreach { line =>
  34. val s_line = line.split('\t')
  35. len += line.length
  36. }
  37. val t2 = System.currentTimeMillis()
  38. val s1 = (t2-t1)/1000.0
  39.  
  40. var mb_s = (len/(1024.0*1024.0))/s1
  41. println(mb_s + " MB/s - Scala split('\\t')")
  42.  
  43. len = 0
  44. val t3 = System.currentTimeMillis()
  45. io.Source.fromFile(args(0)).getLines.foreach { line =>
  46. val s_line = line.split("\t")
  47. len += line.length
  48. }
  49. val t4 = System.currentTimeMillis()
  50. val s2 = (t4-t3)/1000.0
  51.  
  52. mb_s = (len/(1024.0*1024.0))/s2
  53. println(mb_s + " MB/s - Scala split(\"\\t\")")
  54.  
  55. len = 0
  56. val t5 = System.currentTimeMillis()
  57. io.Source.fromFile(args(0)).getLines.foreach { line =>
  58. val s_line = split(line, '\t')
  59. len += line.length
  60. }
  61. val t6 = System.currentTimeMillis()
  62. val s3 = (t6-t5)/1000.0
  63.  
  64. mb_s = (len/(1024.0*1024.0))/s3
  65. println(mb_s + " MB/s - dcsobralWork-1")
  66.  
  67. len = -1
  68. val t7 = System.currentTimeMillis()
  69. io.Source.fromFile(args(0)).getLines.foreach { line =>
  70. val s_line = split2(line, '\t')
  71. len += line.length
  72. }
  73. val t8 = System.currentTimeMillis()
  74. val s4 = (t8-t7)/1000.0
  75.  
  76. mb_s = (len/(1024.0*1024.0))/s4
  77. println(mb_s + " MB/s - dcsobralWork-2")
  78.  
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement