Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #time "on"
  2. type MyRecType() =
  3.  
  4. let list = System.Collections.Generic.List()
  5.  
  6. member this.DoWork() =
  7. let mutable tcs = (System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.Create())
  8. let returnTask = tcs.Task // NB! must access this property first
  9. let mutable local = 1
  10.  
  11. let rec outerLoop() =
  12. if local < 1000000 then
  13. innerLoop(1)
  14. else
  15. tcs.SetResult(local)
  16. ()
  17.  
  18. and innerLoop(inc:int) =
  19. if local % 2 = 0 then
  20. local <- local + inc
  21. outerLoop()
  22. else
  23. list.Add(local) // just fake access to a field to illustrate the pattern
  24. local <- local + 1
  25. innerLoop(inc)
  26.  
  27. outerLoop()
  28.  
  29. returnTask
  30.  
  31.  
  32. let instance = MyRecType()
  33.  
  34. instance.DoWork().Result
  35.  
  36. > Real: 00:00:00.019, CPU: 00:00:00.031, GC gen0: 0, gen1: 0, gen2: 0
  37. > val it : int = 1000001
  38.  
  39. member this.DoWork2() =
  40. let mutable tcs = (System.Runtime.CompilerServices.AsyncTaskMethodBuilder<int>.Create())
  41. let returnTask = tcs.Task // NB! must access this property first
  42. let mutable local = 1
  43. let rec loop(isOuter:bool, inc:int) =
  44. if isOuter then
  45. if local < 1000000 then
  46. loop(false,1)
  47. else
  48. tcs.SetResult(local)
  49. ()
  50. else
  51. if local % 2 = 0 then
  52. local <- local + inc
  53. loop(true,1)
  54. else
  55. list.Add(local) // just fake access to a field to illustrate the pattern
  56. local <- local + 1
  57. loop(false,1)
  58.  
  59. loop(true,1)
  60.  
  61. returnTask
  62.  
  63.  
  64. > Real: 00:00:00.004, CPU: 00:00:00.015, GC gen0: 0, gen1: 0, gen2: 0
  65. > val it : int = 1000001
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement