# How to precisely measure function execution time

**URL:** <https://forum.inductiveautomation.com/t/how-to-precisely-measure-function-execution-time/56618>\
**Category:** Ignition\
**Created:** [February 28, 2022, 2:36pm UTC](https://forum.inductiveautomation.com/t/how-to-precisely-measure-function-execution-time/56618 "2022-02-28T14:36:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hewking32](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@hewking32](https://forum.inductiveautomation.com/u/hewking32)\
**Post date:** [February 28, 2022, 2:36pm UTC](https://forum.inductiveautomation.com/t/how-to-precisely-measure-function-execution-time/56618/1 "2022-02-28T14:36:57Z")

</div>

hi  
I have a piece of code which is quite slow (the code interacts with the database) and need to measure its execution time to know if i make it quicker or slower. Could you please share what tools would you use to measure it?  
Best Regards and thanks for your great help so far!

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [February 28, 2022, 2:40pm UTC](https://forum.inductiveautomation.com/t/how-to-precisely-measure-function-execution-time/56618/2 "2022-02-28T14:40:43Z")

</div>

The easiest would be to add some system.date functions at the beginning and end of the function.

```auto
def myFunc():
    start = system.date.now()
    ....
    end = system.date.now()
    execTime = system.date.millisBetween(start,end)

```

`millisBetween()` should work in most cases, but if you need something for faster scripts you could use  
` java.lang.System.nanoTime()`.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 28, 2022, 3:04pm UTC](https://forum.inductiveautomation.com/t/how-to-precisely-measure-function-execution-time/56618/3 "2022-02-28T15:04:56Z")

</div>

> [@hewking32](#):
>
> I have a piece of code which is quite slow (the code interacts with the database)

Likely that the database is the source of your slowness, depending on what you define as "slow".

I would make sure you/or someone, look at the indexing of involved tables, and query execution plan. DB queries are notorious for unexpected performance issues.

For code execution time, I always use `java.lang.System.nanoTime()`

```auto
from java.lang import System

start = System.nanotime()
# your code
end = System.nanotime()
print 'Took {} nanosecodes'.format(end-start)

```

---

<div class="post-metadata">

**Author:** ![hewking32](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@hewking32](https://forum.inductiveautomation.com/u/hewking32)\
**Post date:** [February 28, 2022, 3:22pm UTC](https://forum.inductiveautomation.com/t/how-to-precisely-measure-function-execution-time/56618/4 "2022-02-28T15:22:23Z")

</div>

that will do the trick thank you

---

<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:** [February 28, 2022, 3:25pm UTC](https://forum.inductiveautomation.com/t/how-to-precisely-measure-function-execution-time/56618/5 "2022-02-28T15:25:50Z")

</div>

Always use `System.nanoTime()` for benchmarking. It uses a monotonic clock that is synchronized across processors, and is immune to system clock adjustments. If your thread gets interrupted and migrated, `.nanoTime()` will be most consistent.

---

<div class="post-metadata">

**Author:** ![hewking32](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@hewking32](https://forum.inductiveautomation.com/u/hewking32)\
**Post date:** [February 28, 2022, 3:41pm UTC](https://forum.inductiveautomation.com/t/how-to-precisely-measure-function-execution-time/56618/6 "2022-02-28T15:41:34Z")

</div>

thanks a lot for your feedback
