Guest User

Untitled

a guest
Nov 24th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. Red []
  2.  
  3. e.g.: :comment
  4.  
  5. delta-time: function [
  6. "Return the time it takes to evaluate a block"
  7. code [block! word! function!] "Code to evaluate"
  8. /count ct "Eval the code this many times, rather than once"
  9. ][
  10. ct: any [ct 1]
  11. t: now/time/precise
  12. if word? :code [code: get code]
  13. loop ct [do code]
  14. now/time/precise - t
  15. ]
  16.  
  17. runs-per: function [
  18. "Return the number of times code can run in a given period"
  19. code [block! word! function!] "Code to evaluate"
  20. time [time!]
  21. ][
  22. t: now/time/precise
  23. n: 0
  24. if word? :code [code: get code]
  25. until [
  26. n: n + 1
  27. do code
  28. now/time/precise - t >= time
  29. ]
  30. n
  31. ]
  32.  
  33. ; Putting the runtime first in results, and memory second, helps things
  34. ; line up nicely. It's a problem if we want to add more stats though,
  35. ; as any code using the data with expected field indexes will break if
  36. ; we don't add the new stats at the end. We could use named fields as
  37. ; well but, for now, we'll stick with this and let this comment serve
  38. ; as a warning. More stats will certainly come in the future, as will
  39. ; GC, but this is just a quickie function in any case.
  40. ; Memory stats and formatted output added by @toomasv.
  41. profile: function [
  42. "Profile code, returning [time memory source] results"
  43. blocks [block!] "Block of code values (block, word, or function) to profile"
  44. /count ct "Eval code this many times, rather than once"
  45. /show "Display results, instead of returning them as a block"
  46. ][
  47. ct: any [ct 1] ; set number of evaluations
  48. baseline: delta-time/count [] ct
  49. res: collect [
  50. foreach blk blocks [
  51. stats-1: stats ; get current stats before evaluation
  52. n: subtract delta-time/count :blk ct baseline
  53. keep/only reduce [
  54. round/to n .001
  55. round/to n / ct .001
  56. stats - stats-1
  57. either block? :blk [copy blk][:blk]
  58. ]
  59. ]
  60. ]
  61. sort res ; sort by time
  62. either show [
  63. print ["Count:" ct]
  64. template: [pad (time) 12 #"|" pad (time-per) 12 #"|" pad (memory) 11 #"|" (mold/flat :code)]
  65. insert/only res ["Time" "Time (Per)" "Memory" Code] ; last column is molded, so not a string here
  66. foreach blk res [
  67. set [time time-per memory code] blk
  68. print compose template
  69. ]
  70. ][
  71. insert/only res compose [count: (ct) fields: [Time Time-Per Memory Code]]
  72. new-line/all res on ; Return formatted results
  73. ]
  74. ]
  75. e.g. [
  76. profile []
  77. profile/show []
  78.  
  79. profile [[wait 1] [wait .25] [wait .5]]
  80. profile/count [[100 / 1 * (100 / 1)] [100.0 / 1.0 ** 2] [100% / 1%]] 1000000
  81.  
  82. one: [1 + 1]
  83. two: [2 + 2]
  84. profile [one two]
  85.  
  86. profile/show [[wait 1] [wait .25] [wait .5]]
  87. profile/show/count [[100 / 1 * (100 / 1)] [100.0 / 1.0 ** 2] [100% / 1%]] 1000000
  88. profile/show [one two]
  89.  
  90. b1: [wait .25]
  91. b2: [wait .5]
  92. profile/show/count reduce [b1 b2] 2
  93.  
  94. f1: does [wait .25]
  95. f2: does [wait .5]
  96. profile/show/count reduce [:f1 :f2] 2
  97.  
  98. ]
Add Comment
Please, Sign In to add comment