Jump to content
  • 0

How Local Variable based code execution is faster than Global Variable based codes ?


GOEMON

Question

7 answers to this question

Recommended Posts

3 hours ago, GOEMON said:

For example:

local a, b, c = 1, 2, 3
local t = os.clock()
for i = 1, n do
    c = a + b
end
t = os.clock() - t
print('use local vars: '..t..' seconds')

 

How does this work fastly  ?

Well , global variable is like publiced thing .
Out of script anything can read it .
When u will write print(_G) this variable will be printed with it's meaning.
Bcs of this such variable will take more time for running for example in large loops , since in lua process its shared everywhere.
And about localed variable.
Its faster since its readable only for inside function.
For example:

(function() 
local a = "hello World" 
print(a)--it will be printed , since its inside function
end)()
print(a)--here a will be nil , bcs its localed variable inside of function and it cant be readen .

 

Link to comment
Share on other sites

Another example:

local T = os.clock()
local a , b = 5 -- a ,b is localed 
for i = 1,10000 do
b = a*a
end
print(os.clock()-T) --in GG (in 6ram phone) its executing in 0.20-0.24 second

Above this case is tested for localed .

And about globaled :

local T = os.clock()
 a , b = 5 -- a ,b is globaled now 
for i = 1,10000 do
b = a*a
end
print(os.clock()-T) --in GG (in 6ram phone) its executing in 0.60-0.80 second

For globaled variables it took 3-4x more time as u can see .

Link to comment
Share on other sites

20 minutes ago, TopGEOYT said:

Well , global variable is like publiced thing .
Out of script anything can read it .
When u will write print(_G) this variable will be printed with it's meaning.
Bcs of this such variable will take more time for running for example in large loops , since in lua process its shared everywhere.
And about localed variable.
Its faster since its readable only for inside function.
For example:


(function() 
local a = "hello World" 
print(a)--it will be printed , since its inside function
end)()
print(a)--here a will be nil , bcs its localed variable inside of function and it cant be readen .

 

Thanks.. it's really helpfull 🤗🤗

Link to comment
Share on other sites

6 minutes ago, TopGEOYT said:

Another example:


local T = os.clock()
local a , b = 5 -- a ,b is localed 
for i = 1,10000 do
b = a*a
end
print(os.clock()-T) --in GG (in 6ram phone) its executing in 0.20-0.24 second

Above this case is tested for localed .

And about globaled :


local T = os.clock()
 a , b = 5 -- a ,b is globaled now 
for i = 1,10000 do
b = a*a
end
print(os.clock()-T) --in GG (in 6ram phone) its executing in 0.60-0.80 second

For globaled variables it took 3-4x more time as u can see .

Ya local variable is best . But im some cases we have to use global too for beter results.

Link to comment
Share on other sites

  • Administrators

Any global variable is search in special global table.

So if you write

a = b

It is mean:

Get global table. Get B from here. Put in register. Get global table. Put data from register to B field.

If both variable is local it is more simple:

Copy from one register to another.

Nothing more.

So because global vars use special table it is slow.

You can write code with and without global vars. Produce .lasm and compare opcodes and its count.

Link to comment
Share on other sites

11 hours ago, Enyby said:

Any global variable is search in special global table.

So if you write

a = b

It is mean:

Get global table. Get B from here. Put in register. Get global table. Put data from register to B field.

If both variable is local it is more simple:

Copy from one register to another.

Nothing more.

So because global vars use special table it is slow.

You can write code with and without global vars. Produce .lasm and compare opcodes and its count.

Ohh.. 🥺

Link to comment
Share on other sites

11 hours ago, Enyby said:

Any global variable is search in special global table.

So if you write

a = b

It is mean:

Get global table. Get B from here. Put in register. Get global table. Put data from register to B field.

If both variable is local it is more simple:

Copy from one register to another.

Nothing more.

So because global vars use special table it is slow.

You can write code with and without global vars. Produce .lasm and compare opcodes and its count.

Bdw where can i contact you ? I couldn't find you on telegram brother 🥺..

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.