# Script Calculation and writing to memory tags

**URL:** <https://forum.inductiveautomation.com/t/script-calculation-and-writing-to-memory-tags/21251>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [December 6, 2018, 6:20am UTC](https://forum.inductiveautomation.com/t/script-calculation-and-writing-to-memory-tags/21251 "2018-12-06T06:20:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kcabatic](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@kcabatic](https://forum.inductiveautomation.com/u/kcabatic)\
**Post date:** [December 6, 2018, 6:20am UTC](https://forum.inductiveautomation.com/t/script-calculation-and-writing-to-memory-tags/21251/1 "2018-12-06T06:20:19Z")

</div>

Hey everyone,

Brief description of what we are trying to do:

- On gateway timer we have our calculation script running twice a second.
- Calculation runs and 300 tags are written using ‘system.tag.writeAll()’
- Same values are written to a ‘.csv’ file
- finally we perform ‘system.tag.scan()’ to force a historical scan

We export the historical data using ‘system.tag.queryTagHistory()’ and ‘system.dataset.exportExcel()’. When we exported the historical data and compare with the ‘.csv’ file we get differences. At least in out minds these two files should be the same.

I think that the tags are not being update fast enough before the historical scan occurs. But I doubt writing to 300 tags will take longer then 100ms.

some questions:

1. Since I am writing to the memory tags via timer does the scanclass (under general properties) of tag properties matter? (how does scan evaluation work?)

2. Is there a way to measure how long system.tag.writeAll() takes to complete all the tags?

Thanks,  
Ken

---

<div class="post-metadata">

**Author:** ![DavidWisely](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/davidwisely/32/10482_2.png) [@DavidWisely](https://forum.inductiveautomation.com/u/DavidWisely)\
**Post date:** [December 6, 2018, 8:27am UTC](https://forum.inductiveautomation.com/t/script-calculation-and-writing-to-memory-tags/21251/2 "2018-12-06T08:27:38Z")

</div>

system.tag.writeAll() is asynchronous and may still be doing the writes in the background when you do the system.tag.scan(). Try using system.tag.writeAllSynchronous() instead.

---

<div class="post-metadata">

**Author:** ![kcabatic](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@kcabatic](https://forum.inductiveautomation.com/u/kcabatic)\
**Post date:** [December 6, 2018, 3:13pm UTC](https://forum.inductiveautomation.com/t/script-calculation-and-writing-to-memory-tags/21251/3 "2018-12-06T15:13:04Z")

</div>

I will give that a go and see what results I get

Do you know of any way to measure the speed of the tag write using either of the functions?

---

<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:** [December 6, 2018, 3:22pm UTC](https://forum.inductiveautomation.com/t/script-calculation-and-writing-to-memory-tags/21251/4 "2018-12-06T15:22:50Z")

</div>

> [@kcabatic](#):
>
> measure the speed

Precision timing is best done like so:

```auto
from java.lang import System
startTS = System.nanoTime()
system.tag.writeAllSynchronous(.....)
endTS = System.nanoTime()

system.util.getLogger("someLogger").infof("writeAllSynchronous() took %dns", endTS-startTS)

```

You will want to collect some statistics before you make decisions based on this, as multitasking can disturb individual calls.

---

<div class="post-metadata">

**Author:** ![kcabatic](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@kcabatic](https://forum.inductiveautomation.com/u/kcabatic)\
**Post date:** [December 13, 2018, 10:56pm UTC](https://forum.inductiveautomation.com/t/script-calculation-and-writing-to-memory-tags/21251/5 "2018-12-13T22:56:15Z")

</div>

Thanks,

This worked really well. Initial statistics are showing the writeAllSynchronous will not affect our timers from running too long.
