TradingView
PineCoders
26 paź 2021 17:18

LibraryStopwatch 

GOLD (US$/OZ)TVC

Opis

Library "LibraryStopwatch"
Provides functions to time the execution of a script.

When timing scripts, keep in mind that the runtime environment is fluid on TradingView. Different servers or server loads will impact execution time.


Look first. Then leap.



stopwatchStats() Times the execution of a script.  
  Returns: A tuple of four values: timePerBarInMs, totalTimeInMs, barsTimed, barsNotTimed

stopwatch() Times the execution of a script.  
  Returns: A single value: The time elapsed since the beginning of the script, in ms.
Komentarze
joelly3d
Awesome, been asking for something like this for a while now!
Thanks for the hookup!
A_Traders_Edge
Sorry to bother you guys but why do i keep getting the error "line 103: Could not find function or function reference 'stopwatchStats'" when im using the exact function and table code that is in the example (with the "script code to time" being removed and inserting my code in its place?
PineCoders
@chasinalts, Not sure. Starting from this script, replacing the `library()` declaration statement with `indicator()` and removing the two `export` keywords works OK here.
A_Traders_Edge
@PineCoders, My apologies my friend. The issue was not understanding how to properly reference the function from the library when using it in my code. Last question though, I tend to max out my security calls, does using security calls cause the same issues as before with the Stopwatch code that was from AlexGrover (I believe but could be wrong here) that was referenced in the PineCoders FAQ?
PineCoders
@chasinalts, Happy you got things running. Stopwatch code was by LucF and yes, the limitations with `request.security()` still apply, afawk. Those calls are handled with something similar to concurrent threads, so their execution escapes the realm of what is visible from the stopwatch functions.
Canundo
I tried to generally calculate the speed of one script run. And I mean the speed with which one script is calculated from first to last line of code. However, it seems that zero ms passed from start to end.

What I tried to do is figure out which functions or code parts draw a lot of calculation time. I wanted to debug a Stop Loss option in my script that leads to a timeout on total script run.

So I wanted to identify the commands responsible so that I can try to improve them.

Is this generally not possible or am I doing something wrong? Is the value of timenow only set once at the beginning of the script?
Canundo
Nevermind, it works as intended. Very different ms times per run but still helping.
jason5480
Great library and very useful "argument solver" over various performance related arguments and inquiries I had over the years. However, I think I noticed an improvement that could be made. In the implementation of the `stopwatchStats` and `stopwatch` functions the `if ta.change(timeElapsed)` can be replaced by `if not barstate.islast` since the timeElapsed variable will change only if the bar is not the last. In this case all three `if not barstate.islast` checks can be merged into one if block that has the main logic. Two if checks less means that the execution of the time measuring functions will be even less than the overall execution of a script and will result in more accurate results. Could you please evaluate my finding and approve or reject this change? Thanks in advance.
intergoro
Hi, very nice idea!
Would you be able to extend the library in order to include a sort of "tic toc" function (like in R Studio)?
It would be amazing to check, for every part of the script, what are the parts that require more time to be executed so that one knows more precisely where to improve (if possible) the efficiency of the script!
CannyTolany01
GooooD..~~
Więcej