Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- //@author=LucF, for PineCoders
- study("Execution Time", "", true)
- loopCount = input(40000, "Loop iterations per bar") //4000000
- showLabel = input(false)
- // Get elapsed time if end of chart hasn't been reached yet.
- f_msElapsed() =>
- // Get time at first bar.
- var _timeBegin = timenow
- var _timeElapsed = 0.
- // Get ms elapsed since first bar.
- if not barstate.islast
- _timeElapsed := timenow - _timeBegin
- _timeElapsed
- ms = f_msElapsed()
- // Heat up the CPUs.
- var a = 0
- for i = 0 to loopCount
- a := a + i
- // Calculate avg/bar only when time changes.
- var msPerBar = 0.
- var barsTimed = 0
- var barsNotTimed = 0
- if change(ms)
- barsTimed := bar_index + 1
- msPerBar := ms / barsTimed
- // After a time change, estimate additional time elapsed using avg time per bar.
- if not barstate.islast
- // Bars elapsed since last change of time.
- barsNotTimed := bar_index + 1 - barsTimed
- // Add avg bar time to bars since time change to get better estimate of total time elapsed.
- totalTime = ms + (barsNotTimed * msPerBar)
- // Plotting
- msPerBarColor = msPerBar > 50 ? color.red : msPerBar > 5 ? color.maroon : msPerBar == 0 ? color.lime : color.green
- if barstate.islast and showLabel
- var label lbl = na
- label.delete(lbl[1])
- 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)
- plotchar(totalTime, "Execution time (ms)", "", location.top)
- plotchar(msPerBar, "Avg time / bar (ms)", "", location.top, msPerBarColor)
- plotchar(barsTimed + barsNotTimed, "Bars", "", location.top)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement