# Script Console Execution Time

**URL:** <https://forum.inductiveautomation.com/t/script-console-execution-time/32909>\
**Category:** Ignition\
**Created:** [March 7, 2020, 9:54am UTC](https://forum.inductiveautomation.com/t/script-console-execution-time/32909 "2020-03-07T09:54:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mori.luca](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mori.luca/32/13424_2.png) [@mori.luca](https://forum.inductiveautomation.com/u/mori.luca)\
**Post date:** [March 7, 2020, 9:54am UTC](https://forum.inductiveautomation.com/t/script-console-execution-time/32909/1 "2020-03-07T09:54:44Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 7, 2020, 3:00pm UTC](https://forum.inductiveautomation.com/t/script-console-execution-time/32909/2 "2020-03-07T15:00:45Z")

</div>

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()](https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#nanoTime--) for more precision for quick functions.

Something like this in a shared script module:

```auto
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`.

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [March 7, 2020, 3:34pm UTC](https://forum.inductiveautomation.com/t/script-console-execution-time/32909/3 "2020-03-07T15:34:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mori.luca](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mori.luca/32/13424_2.png) [@mori.luca](https://forum.inductiveautomation.com/u/mori.luca)\
**Post date:** [March 7, 2020, 5:11pm UTC](https://forum.inductiveautomation.com/t/script-console-execution-time/32909/4 "2020-03-07T17:11:34Z")

</div>

@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

```auto
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.

---

<div class="post-metadata">

**Author:** ![mori.luca](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mori.luca/32/13424_2.png) [@mori.luca](https://forum.inductiveautomation.com/u/mori.luca)\
**Post date:** [March 7, 2020, 5:14pm UTC](https://forum.inductiveautomation.com/t/script-console-execution-time/32909/5 "2020-03-07T17:14:53Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 7, 2020, 6:18pm UTC](https://forum.inductiveautomation.com/t/script-console-execution-time/32909/6 "2020-03-07T18:18:02Z")

</div>

> [@mori.luca](#):
>
> 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

Python has standard features in [function definitions](https://docs.python.org/2.7/reference/compound_stmts.html#function-definitions) for this problem, [args and keyword-args](https://www.digitalocean.com/community/tutorials/how-to-use-args-and-kwargs-in-python-3)--I left them out thinking you would use them. Adjust my script like so:

```auto
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:

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

```

you can instead use:

```auto
answer = shared.someModule.timeMe(runMe, arg1, someOtherArg, someKeyword=anotherArg)

```

---

<div class="post-metadata">

**Author:** ![mori.luca](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mori.luca/32/13424_2.png) [@mori.luca](https://forum.inductiveautomation.com/u/mori.luca)\
**Post date:** [March 7, 2020, 10:34pm UTC](https://forum.inductiveautomation.com/t/script-console-execution-time/32909/7 "2020-03-07T22:34:29Z")

</div>

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.
