Global Variable vs. Global Function
I’m not sure if this is the best or most accurate way to test. But thought you might still be interested anyway.
Local Only (Control Group) - 4sec in Simulator: main.lua
[lua]–local test = require(“speedTest”)
local x = 0
–x = 0
print(os.time())
while x < 100000000 do
–x = test:set()
–_G.x = _G.x+1
x = x+1
end
print(x)
–print(_G.x)
print(os.time())[/lua]
Global Variable Test - 25sec in Simulator: main.lua
[lua]–local test = require(“speedTest”)
–local x = 0
x = 0
print(os.time())
while x < 100000000 do
–x = test:set()
_G.x = _G.x+1
–x = x+1
end
–print(x)
print(_G.x)
print(os.time())[/lua]
Global Variable without _G - 15sec in Simulator: main.lua
[lua]–local test = require(“speedTest”)
–local x = 0
x = 0
print(os.time())
while x < 100000000 do
–x = test:set()
–_G.x = _G.x+1
x = x+1
end
print(x)
–print(_G.x)
print(os.time())[/lua]
Global Function Test - 17sec in Simulator: main.lua
[lua]local test = require(“speedTest”)
local x = 0
–x = 0
print(os.time())
while x < 100000000 do
x = test:set()
–_G.x = _G.x+1
–x = x+1
end
print(x)
–print(_G.x)
print(os.time())[/lua]
speedTest.lua
[lua]module(…, package.seeall)
local x = 0
function speedTest:set()
x = x+1
return x
end[/lua]
I have never been able to access global variables outside of my main.lua without _G, so I’m not sure that test even matters but its good for reference. I guess using your Global Function to set Local Variable is the fastest way. Thanks! [import]uid: 9187 topic_id: 3822 reply_id: 12533[/import]