Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. open FParsec
  2. open FParsec.CharParsers
  3.  
  4. open System.Text.RegularExpressions
  5.  
  6. // now () returns current time in milliseconds since start
  7. let now : unit -> int64 =
  8. let sw = System.Diagnostics.Stopwatch ()
  9. sw.Start ()
  10. fun () -> sw.ElapsedMilliseconds
  11.  
  12. // time estimates the time 'action' repeated a number of times
  13. let time repeat action =
  14. let inline cc i = System.GC.CollectionCount i
  15.  
  16. let v = action ()
  17.  
  18. System.GC.Collect (2, System.GCCollectionMode.Forced, true)
  19.  
  20. let bcc0, bcc1, bcc2 = cc 0, cc 1, cc 2
  21. let b = now ()
  22.  
  23. for i in 1..repeat do
  24. action () |> ignore
  25.  
  26. let e = now ()
  27. let ecc0, ecc1, ecc2 = cc 0, cc 1, cc 2
  28.  
  29. v, (e - b), ecc0 - bcc0, ecc1 - bcc1, ecc2 - bcc2
  30.  
  31. let regex = Regex ( @"^\s*(?<id>\w+)\s*=\s*(?<val>\d+)\s*$"
  32. , RegexOptions.Compiled ||| RegexOptions.ExplicitCapture ||| RegexOptions.Singleline ||| RegexOptions.CultureInvariant
  33. )
  34.  
  35. let rparse s =
  36. let m = regex.Match s
  37. if m.Success then
  38. let i = m.Groups.["id"].Value
  39. let v = m.Groups.["val"].Value |> int
  40. Some (i, v)
  41. else
  42. None
  43.  
  44. let fparser : Parser<_, unit> =
  45. spaces
  46. >>. manyChars CharParsers.letter
  47. .>> spaces
  48. .>> skipChar '='
  49. .>> spaces
  50. .>>. pint32
  51. .>> spaces
  52. .>> eof
  53.  
  54. let fparse s =
  55. match runParserOnString fparser () "Input" s with
  56. | Success (v, _, _) -> Some v
  57. | Failure (s, _, _) -> None
  58.  
  59. let inline isChar ch = (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')
  60. let inline isDigit ch = ch >= '0' && ch <= '9'
  61.  
  62. let z = 0
  63.  
  64. type Parser(input : string) =
  65. let mutable pos = 0
  66. let mutable id = Array.zeroCreate 32
  67. let mutable idpos = 0
  68. let mutable value = 0
  69.  
  70. member x.spaces () =
  71. let rec loop () =
  72. if pos < input.Length && input.[pos] = ' ' then
  73. pos <- pos + 1
  74. loop ()
  75. loop ()
  76. true
  77.  
  78. member x.skipChar ch =
  79. if pos < input.Length && input.[pos] = ch then
  80. pos <- pos + 1
  81. true
  82. else
  83. false
  84.  
  85. member x.identifier () =
  86. let rec loop () =
  87. if pos < input.Length then
  88. let ch = input.[pos]
  89. if isChar ch then
  90. id.[idpos] <- ch
  91. idpos <- idpos + 1
  92. pos <- pos + 1
  93. loop ()
  94. let prev = pos
  95. loop ()
  96. prev < pos
  97.  
  98. member x.integer () =
  99. let rec loop () =
  100. if pos < input.Length then
  101. let ch = input.[pos]
  102. if isDigit ch then
  103. value <- value*10 + int ch - int '0'
  104. pos <- pos + 1
  105. loop ()
  106. let prev = pos
  107. loop ()
  108. prev < pos
  109.  
  110. member x.eof () =
  111. pos >= input.Length
  112.  
  113. member x.parse () =
  114. let result =
  115. x.spaces ()
  116. && x.identifier ()
  117. && x.spaces ()
  118. && x.skipChar '='
  119. && x.spaces ()
  120. && x.integer ()
  121. && x.spaces ()
  122. && x.eof ()
  123. if result then
  124. Some (System.String (id, 0, idpos), value)
  125. else
  126. None
  127.  
  128. let cparse s =
  129. let p = Parser s
  130. p.parse ()
  131.  
  132.  
  133.  
  134. let testCases =
  135. [|
  136. 100000 , "x=3"
  137. 100000 , " abcdef = 2930932 "
  138. 100000 , " abcdefghijklmnopqrst = 2930932 "
  139. 100000 , "x="
  140. 100000 , " abcdef = 2930932 aa"
  141. |]
  142.  
  143. [<EntryPoint>]
  144. let main argv =
  145.  
  146. for repeat, expr in testCases do
  147. printfn "Running test case %A (iterations: %d)" expr repeat
  148. let rresult, rtime, rcc0, rcc1, rcc2 = time repeat (fun () -> rparse expr)
  149. let fresult, ftime, fcc0, fcc1, fcc2 = time repeat (fun () -> fparse expr)
  150. let cresult, ctime, ccc0, ccc1, ccc2 = time repeat (fun () -> cparse expr)
  151. printfn "Regex result : %A (time: %d, cc0: %d, cc1: %d, cc2: %d)" rresult rtime rcc0 rcc1 rcc2
  152. printfn "FParsec result : %A (time: %d, cc0: %d, cc1: %d, cc2: %d)" fresult ftime fcc0 fcc1 fcc2
  153. printfn "Custom result : %A (time: %d, cc0: %d, cc1: %d, cc2: %d)" cresult ctime ccc0 ccc1 ccc2
  154. printfn "Results are %s" (if rresult = fresult && rresult = cresult then "EQUAL" else "NOT EQUAL")
  155. 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement