# Execution time of Lua script in Defold is VERY high compared to execution through terminal

**URL:** <https://forum.defold.com/t/execution-time-of-lua-script-in-defold-is-very-high-compared-to-execution-through-terminal/75872>\
**Category:** Questions\
**Created:** [February 1, 2024, 10:56am UTC](https://forum.defold.com/t/execution-time-of-lua-script-in-defold-is-very-high-compared-to-execution-through-terminal/75872 "2024-02-01T10:56:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaNDaZzLe](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/randazzle/32/34866_2.png) [@RaNDaZzLe](https://forum.defold.com/u/RaNDaZzLe)\
**Post date:** [February 1, 2024, 10:56am UTC](https://forum.defold.com/t/execution-time-of-lua-script-in-defold-is-very-high-compared-to-execution-through-terminal/75872/1 "2024-02-01T10:56:58Z")

</div>

**Description**  
Running 10000000 loops in a Lua module script using **LuaJIT 2.1.0-beta3** in the terminal gives 10.62 seconds.  
Using the same module in Defold by requiring the same module gives execution time of 3 minutes.  
Can we know what is causing this and if there is any way we can get around this? This is very crucial for our project and we would appreciate any effort from anyone answering this question.

**To Reproduce**  
Steps to reproduce the behavior:

1. Make a Lua module with following script:

```lua
local M = {}

local function run_loop()
    for i = 1, 10000000 do
        print(i)
    end
end

run_loop()

return M

```

1. Run it in terminal using “luajit .lua” and evaluate time taken.
2. Require same Lua module in main.script in an empty Mobile Game template Defold project and evaluate again.

**Defold version:**

- Version : 1.6.2

**Platforms:**

- Platforms: macOS, Android
- OS: MacOS 13.3.1, AndroidOS
- Device: MacBook Air M1, Any Android Device

---

<div class="post-metadata">

**Author:** ![d954mas](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/d954mas/32/5401_2.png) [@d954mas](https://forum.defold.com/u/d954mas)\
**Post date:** [February 1, 2024, 11:00am UTC](https://forum.defold.com/t/execution-time-of-lua-script-in-defold-is-very-high-compared-to-execution-through-terminal/75872/2 "2024-02-01T11:00:32Z")

</div>

I think print can be problem. If you run debug build or build in editor, it will use log system for prints.

What if you replace print with some math?

---

<div class="post-metadata">

**Author:** ![RaNDaZzLe](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/randazzle/32/34866_2.png) [@RaNDaZzLe](https://forum.defold.com/u/RaNDaZzLe)\
**Post date:** [February 2, 2024, 4:34am UTC](https://forum.defold.com/t/execution-time-of-lua-script-in-defold-is-very-high-compared-to-execution-through-terminal/75872/3 "2024-02-02T04:34:18Z")

</div>

I did as you said. I put a simple 1 + 1 inside the loop and removed the print as so. I even added a simple execution time calculator.

```Lua
local M = {}

local function run_loop()
	local time_start = os.time()
	for i = 1, 100000000 do
		local a = 1 + 1
	end
	print("End time: ", os.time() - time_start)
end

run_loop()

return M

```

The time taken to execute has reduced by a lot.  
LuaJIT through terminal → 0 second (probably has float value, but it was very fast)  
Defold → 35 seconds

But as you can see, the issue still persists. Execution in Defold is taking a lot more time.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [February 2, 2024, 6:21am UTC](https://forum.defold.com/t/execution-time-of-lua-script-in-defold-is-very-high-compared-to-execution-through-terminal/75872/4 "2024-02-02T06:21:12Z")

</div>

It looks like it is the jit compilation which messes with the execution of that loop. Disable jit compilation and it is as fast as you’d expect:

```lua
if jit then jit.off() end
run_loop()
if jit then jit.on() end

```

---

<div class="post-metadata">

**Author:** ![RaNDaZzLe](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/randazzle/32/34866_2.png) [@RaNDaZzLe](https://forum.defold.com/u/RaNDaZzLe)\
**Post date:** [February 2, 2024, 9:23am UTC](https://forum.defold.com/t/execution-time-of-lua-script-in-defold-is-very-high-compared-to-execution-through-terminal/75872/6 "2024-02-02T09:23:53Z")

</div>

Thank you! This worked for us. But it looks too good to be true. So I would like to know what are the downsides of doing this.

I’m also curious as to why the JIT compilation is causing this. Would appreciate it if you could briefly explain.

---

<div class="post-metadata">

**Author:** ![iotaHum](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/i/977dab/32.png) [@iotaHum](https://forum.defold.com/u/iotaHum)\
**Post date:** [February 2, 2024, 10:23am UTC](https://forum.defold.com/t/execution-time-of-lua-script-in-defold-is-very-high-compared-to-execution-through-terminal/75872/7 "2024-02-02T10:23:47Z")

</div>

I guess switching of JIT reduces start up time for your test.  
Your test is a good first step towards thinking about profiling. Of itself the test is not useful, what is actually being tested is opaque (a compiler might replace 1+1 with 2 or even remove the loop completely, who knows). Then there is relating ‘results’ to what you really want to do. Better to profile your actual game:

> **[Profiling in Defold](https://defold.com/manuals/profiling/?q=time)**
>
> This manual explains the profiling facilities present in Defold.
