# Is there anyway to further optimize calculation speed?

**URL:** <https://community.thunkable.com/t/is-there-anyway-to-further-optimize-calculation-speed/2857543>\
**Category:** Questions about Thunkable X\
**Created:** [February 26, 2024, 2:06pm UTC](https://community.thunkable.com/t/is-there-anyway-to-further-optimize-calculation-speed/2857543 "2024-02-26T14:06:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vincentking700](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/vincentking700/32/106716_2.png) [@vincentking700](https://community.thunkable.com/u/vincentking700)\
**Post date:** [February 26, 2024, 2:06pm UTC](https://community.thunkable.com/t/is-there-anyway-to-further-optimize-calculation-speed/2857543/1 "2024-02-26T14:06:07Z")

</div>

I am fairly devastated because I believe I reach a point when thunkable is fundamentally incapable to fulfill my quest.

I am trying to perform a 4-layer nested loop combination that aims to calculate a number. The initial data looked like this:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/9/3/93d9e3fdc66b8129bd058fe5e196f68cb0643692.png)

This is an election simulation app. For each county, each racial group, each candidate, there will be a sentiment for each factor, indicating that person’s support on a particulat front; for each county, each racial group, there will be an importance, indicating how each factor is valued; there are also enthusiasm attached to each county serving as turnout modifier.

The task is simple, I need to multiple sentiment, enthusiasm and importance together, then multiply accordingly with demographic, TLDR, The center line in the loop will be performed 3000 (counties) x 28 (stratifying groups) x 7 (factors) x 3 (candidates) = around 2 million runs. Initially the center line was a math formula, but I replaced it as a mere simple “set X to 0” to test out its speed.

Unfortunately for me, without the need to run 2 million times, simply running a 5 county example JSON file, which only contains 3,333 runs, takes 5.9 seconds. Obviously the speed is device dependent, but I feel like averaging 600 multiplying task/ sec is not optimal for a platform that I continuously paid for around two years and spent countless hours working on this one project.

Please refer to the picture below for seeing my block:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/1/0/1033ce8aaa38e8b52c49f7c9b810ae1b65a72fc8.png)

I am very experienced in using thunkable, so I probably have already used all the known tricks on the table. For example, I do know “Set var to var+x” is FASTER than  
“Change var by x”, so in this loop I used the latter; I also know that procedural blocks are considerably slower than direct using blocks; and that stored variable is slower than app variable.

I even know that “wait 0 second” is a terrible thing to use in a loop because it will hundred or thousand fold increase the time it takes, as 0 second actually eats up a few milliseconds.

At this point I am dejected and call for help to see if anyone can help me on optimizing my code, although I am generally convinced that this is the edge of power of thunkable, or my hardware. (I am using IPAD MINI 6THgen and IPhone 15 pro for reference.)

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [February 26, 2024, 3:53pm UTC](https://community.thunkable.com/t/is-there-anyway-to-further-optimize-calculation-speed/2857543/2 "2024-02-26T15:53:54Z")

</div>

> [@vincentking700](#):
>
> The task is simple, I need to multiple sentiment, enthusiasm and importance together, then multiply accordingly with demographic, TLDR, The center line in the loop will be performed 3000 (counties) x 28 (stratifying groups) x 7 (factors) x 3 (candidates) = around 2 million runs.

I think it’s interesting that you describe the task as “simple.” Perhaps it’s simple for you but what you’re doing is complex and nested loops are always going to come with a time consequence.

Can you do this outside of Thunkable? I wonder if a Javascript would be faster?

I guess I wonder a little why you’re using Thunkable for this. Surely there’s a server-based option that’s better.

---

<div class="post-metadata">

**Author:** ![vincentking700](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/vincentking700/32/106716_2.png) [@vincentking700](https://community.thunkable.com/u/vincentking700)\
**Post date:** [February 26, 2024, 3:59pm UTC](https://community.thunkable.com/t/is-there-anyway-to-further-optimize-calculation-speed/2857543/3 "2024-02-26T15:59:41Z")

</div>

Simple as in concept is easy to grasp. To be fair I have limited knowledge outside Thunkable and not until recently do I realize I must consider execute speed , not just the block make sense theoretically. Guess I have a lot to learn!

---

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)\
**Post date:** [February 27, 2024, 12:40am UTC](https://community.thunkable.com/t/is-there-anyway-to-further-optimize-calculation-speed/2857543/4 "2024-02-27T00:40:50Z")

</div>

If I was you, I’d offload this to a cloudflare worker or even use a pure function in a html file in the webviewer and execute js

Much much faster in pure js

---

<div class="post-metadata">

**Author:** ![vincentking700](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/vincentking700/32/106716_2.png) [@vincentking700](https://community.thunkable.com/u/vincentking700)\
**Post date:** [February 27, 2024, 1:16am UTC](https://community.thunkable.com/t/is-there-anyway-to-further-optimize-calculation-speed/2857543/5 "2024-02-27T01:16:34Z")

</div>

Thanks for the tip. As I have never used anything outside Thunkable except knowing very briefly of C#, are there some keywords or quick go to I may rely on that you may enlighten me?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/f/0f59f292712368bce16ff80133ae10de8a6f27e8.png) [@system](https://community.thunkable.com/u/system)\
**Post date:** [May 27, 2024, 1:17am UTC](https://community.thunkable.com/t/is-there-anyway-to-further-optimize-calculation-speed/2857543/6 "2024-05-27T01:17:12Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
