Advertisement
PineCoders

Execution Time

Nov 15th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. //@version=4
  2. //@author=LucF, for PineCoders
  3. study("Execution Time", "", true)
  4. loopCount = input(40000, "Loop iterations per bar") //4000000
  5. showLabel = input(false)
  6.  
  7. // Get elapsed time if end of chart hasn't been reached yet.
  8. f_msElapsed() =>
  9. // Get time at first bar.
  10. var _timeBegin = timenow
  11. var _timeElapsed = 0.
  12. // Get ms elapsed since first bar.
  13. if not barstate.islast
  14. _timeElapsed := timenow - _timeBegin
  15. _timeElapsed
  16.  
  17. ms = f_msElapsed()
  18.  
  19. // Heat up the CPUs.
  20. var a = 0
  21. for i = 0 to loopCount
  22. a := a + i
  23.  
  24. // Calculate avg/bar only when time changes.
  25. var msPerBar = 0.
  26. var barsTimed = 0
  27. var barsNotTimed = 0
  28. if change(ms)
  29. barsTimed := bar_index + 1
  30. msPerBar := ms / barsTimed
  31.  
  32. // After a time change, estimate additional time elapsed using avg time per bar.
  33. if not barstate.islast
  34. // Bars elapsed since last change of time.
  35. barsNotTimed := bar_index + 1 - barsTimed
  36. // Add avg bar time to bars since time change to get better estimate of total time elapsed.
  37. totalTime = ms + (barsNotTimed * msPerBar)
  38.  
  39. // Plotting
  40. msPerBarColor = msPerBar > 50 ? color.red : msPerBar > 5 ? color.maroon : msPerBar == 0 ? color.lime : color.green
  41. if barstate.islast and showLabel
  42. var label lbl = na
  43. label.delete(lbl[1])
  44. lbl := label.new(bar_index, high + tr, tostring(msPerBar, "#.####") + " ms/BAR\nTime Elapsed: " + tostring(totalTime, "#.####") + " ms\nBars Analyzed: " + tostring(barsTimed + barsNotTimed), color=msPerBarColor, textcolor=#00FF00ff, style=label.style_none)
  45. plotchar(totalTime, "Execution time (ms)", "", location.top)
  46. plotchar(msPerBar, "Avg time / bar (ms)", "", location.top, msPerBarColor)
  47. plotchar(barsTimed + barsNotTimed, "Bars", "", location.top)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement