Jump to content
GOEMON

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

Recommended Posts

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  ?

Share this post


Link to post
Share on other sites

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 .

 

Share this post


Link to post
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 .

Share this post


Link to post
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 🤗🤗

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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.. 🥺

Share this post


Link to post
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 🥺..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...