Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check the time execution for my code?

Is there a way to check the time that piece of the pinescript code takes to execute/run?

And I was wondering for the same code if there is any difference if I run it as a plain code in my script or if I make it a export in a library and then call it? I could not find any advantages of using library in terms of speed?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the library published by PineCoders. It is basically a stopwatch

//@version=5
//@author=LucF, for PineCoders

// @description Provides functions to time the execution of a script.
library("LibraryStopwatch")

loopCount = input.int(100000, "Loop iterations per bar", minval = 0, step = 10000)

// @function Times the execution of a script.
// @returns A tuple of four values: [timePerBarInMs, totalTimeInMs, barsTimed, barsNotTimed]
export stopwatchStats() =>
    // Get time at first bar.
    var int timeBegin = timenow
    // Get ms elapsed since first bar.
    var int timeElapsed = 0
    if not barstate.islast
        timeElapsed := timenow - timeBegin
    // Calculate avg/bar only when time changes.
    var float timePerBarInMs = 0.
    // Total bars timed before last change in "timenow".
    var int barsTimed = 0
    // ————— Bars elapsed since last change in "timenow".
    var int barsNotTimed = 0
    if ta.change(timeElapsed)
        barsTimed := bar_index + 1
        timePerBarInMs  := timeElapsed / barsTimed
    // ————— In between time changes, estimate elapsed time using avg time per bar.
    if not barstate.islast
        // Bars elapsed since last change of time.
        barsNotTimed := bar_index  + 1 - barsTimed
    // ————— Add (bars since "timenow" change * avg bar time) to time elapsed since last "timenow" change to get better estimate of total time elapsed.
    float totalTimeInMs = timeElapsed + (barsNotTimed * timePerBarInMs)
    [timePerBarInMs, totalTimeInMs, barsTimed, barsNotTimed]

// @function    Times the execution of a script.
// @returns     A single value: The time elapsed since the beginning of the script, in ms.
export stopwatch() =>
    // Get time at first bar.
    var int timeBegin = timenow
    // Get ms elapsed since first bar.
    var int timeElapsed = 0
    if not barstate.islast
        timeElapsed := timenow - timeBegin
    // Calculate avg/bar only when time changes.
    var float timePerBarInMs = 0.
    // Total bars timed before last change in "timenow".
    var int barsTimed = 0
    // ————— Bars elapsed since last change in "timenow".
    var int barsNotTimed = 0
    if ta.change(timeElapsed)
        barsTimed := bar_index + 1
        timePerBarInMs  := timeElapsed / barsTimed
    // ————— In between time changes, estimate elapsed time using avg time per bar.
    if not barstate.islast
        // Bars elapsed since last change of time.
        barsNotTimed := bar_index  + 1 - barsTimed
    // ————— Add (bars since "timenow" change * avg bar time) to time elapsed since last "timenow" change to get better estimate of total time elapsed.
    float totalTimeInMs = timeElapsed + (barsNotTimed * timePerBarInMs)

// Examples of calls to both functions.
[timePerBarInMs, totalTimeInMs, barsTimed, barsNotTimed] = stopwatchStats()
msElapsed = stopwatch()

// ————— Script code to time.
var a = 0.
for i = 0 to loopCount
    a := a + i + close + math.pow(hl2, 1 / i) + math.avg(close, open) + math.cos(hlc3)

// —————————— Display results
// ————— Print table at the end of chart.
if barstate.islast
    var table t = table.new(position.middle_right, 1, 1)
    var txt = str.tostring(timePerBarInMs, "ms/bar: #.######\n") +
              str.tostring(totalTimeInMs, "Total time (ms): #,###.######\n") + 
              str.tostring(barsTimed + barsNotTimed, "Bars analyzed: #")
    table.cell(t, 0, 0, txt, bgcolor = color.yellow, text_halign = text.align_right)

// ————— Plot elapsed time.
plot(totalTimeInMs,  "totalTimeInMs", color.gray)
plot(msElapsed,      "msElapsed",     color.new(color.gray, 70), 6)

// ————— Print Data Window values.
plotchar(timePerBarInMs,    "Avg time / bar (ms)",  "", location.top)
plotchar(totalTimeInMs,     "Execution time (ms)",  "", location.top)
plotchar(barsTimed,         "Bars timed",           "", location.top)
plotchar(barsNotTimed,      "Bars not timed",       "", location.top)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading