Script Console Execution Time

Hi,
often is necessary to check the execution time of functions.
Of course it can be accomplished by those lines :

start = system.date.now()	
function()
end = system.date.now()	
print "exec in ms : " + str(system.date.millisBetween(start,end))

Is there a way to implement this in the console so without writing every time those line by hands, it show automatically the execution time when it finish to execute the code?

Thanks

No, but I don’t see any reason you couldn’t make your own function to do so, that accepts a function to run as an argument. Or a string to interpret as a statement (via python’s exec statement). I’d use java’s System.nanoTime() for more precision for quick functions.

Something like this in a shared script module:

from java.lang import System

def timeMe(runMe):
    started = System.nanoTime()
    runMe()
    finished = System.nanoTime()
    print "Completed in %d ns" % (finished - started)

You can expand on that to pass arguments or use exec.

1 Like

Further to Phil’s comments, I like to use the system.util.logger to report on job’s starting and finishing - especially if they are long-running background (invoked Async) tasks.

If something should have run and hasn’t run right, type your logger ID into the log console and you will get back the start of an event, etc.

Useful if you have a task that is split into e.g. 10 functions, that get called in order, or even for larger jobs that are split across scripts.

2 Likes

@pturmel Thanks for you reply, I actually thought to use a function, passing as parameter the name of a script, but then I think I have to encapsulate my old functions parameters in arrays or dictionary, so I can call them easily by passing to timeMe(functionName, dict)
Then :

from java.lang import System

def timeMe(runMe, dict):
    started = System.nanoTime()
    runMe(dict)
    finished = System.nanoTime()
    print "Completed in %d ns" % (finished - starte

I actually have pro and cons, I will gain more flexibility with a parameter like a dictionary, but I will need to check inside the function if I have all the requested field for a given function ( which sound good anyway, so I actually think from next project I could go this way ).

As always thanks for your time.

@Matrix_Engineering That’s definitely a reliable solutions for runtime debug if needed.
My aim was something to quickly test script from the console scope.
Thanks anyway for your answer tough, it is an useful one for sure.

1 Like

Python has standard features in function definitions for this problem, args and keyword-args--I left them out thinking you would use them. Adjust my script like so:

from java.lang import System

def timeMe(runMe, *args, **kwargs):
    started = System.nanoTime()
    _retv = runMe(*args, **kwargs)
    finished = System.nanoTime()
    print "Completed in %d ns" % (finished - started)
    return _retv

So, wherever you would call your function like so:

answer = runMe(arg1, someOtherArg, someKeyword=anotherArg)

you can instead use:

answer = shared.someModule.timeMe(runMe, arg1, someOtherArg, someKeyword=anotherArg)
3 Likes

Oh… i see.
I feel is about time to start study python in depth since I will use it a lot in ignition.
Thanks for your input.