Gmod Lua Modules

Sorry for the rather short question but here goes.

I’m wondering if using a module (ex: module( ‘foo’, package.seeall ) ) offers any performance benafits to scripts? I’m writing some library functions that do alot of very tight looping and I’m wondering if restricting the variable space would be worth the effort.
(to put this in perspective the code is looping about 40 million or more times per second for an iMesh vertex structure generation algorithm)

Uh… I may have some bad news for you OP

lmao no it’s not

-snip-

I’m generating a vertex mesh structure for a chunk in my garrysmod minecraft gamemode. It has to do a very large amount of processing to calculate lighting through diffusion etc, and other post processing. here is a link to the project this is a part of:

http://forum.facepunch.com/showthread.php?t=1272317

honestly annoys me how you guys dismiss threads without even giving an answer. I’m not a retard, I do know what I’m doing and am, infact, probably attempting something more difficult than most of you have tried so I’d appreciate you not using this thread to stroke your egos and post useless replies that don’t give a serious answer.

(the 40 million times per second statistic is an example from bench-marking at maximum speed. The algorithm is about 30 times faster than it needs to be to allow it to run at reasonable in-game FPS rates)

Using module() shouldn’t change the framerate or tickrate, because you should only call it once.

but that’s not quite what I’m asking. What i’m asking is if, by using module() to restrict the variable space (i’m lead to understand it creates a copy of the global var table) there are any speed benefits to be had, or is the use of module() just something to do with keeping the code self contained and preventing any of it’s functions from “escaping” and messing with other scripts.

Pretty sure that is the reason for module(), nothing else.

Also, have some speedtests…



] lua_openscript_cl time.lua
Running script time.lua...
Started tests for func 1 (833.231881)
Ended tests for func 1 (833.237492) [0.005611 secs]
Started tests for func 2 (833.237567)
Ended tests for func 2 (833.633839) [0.396272 secs]
Started tests for func 3 (833.633890)
Ended tests for func 3 (833.925146) [0.291256 secs]


used with:



local time = SysTime;

local rand = math.random
local amt = 10000000;

local start = "Started tests for func %i (%f)";
local e = "Ended tests for func %i (%f) [%f secs]";

do
	module("aa", package.seeall);
	blob = true;
	toggle = function() blob = not blob; end
end

local bb = {};
bb.toggle = function() bb.blob = not bb.blob; end

local c = true;
local laa = aa;
local funcs = {
	[1] = function() 
		bb.toggle();
	end;
	[2] = function() 
		aa.toggle();
	end;
	[3] = function() 
		laa.toggle();
	end
}

for i = 1, #funcs do
	local s = time()
	print(string.format(start, i, s));
	local f = funcs*;
	for i = 1, amt do
		f();
	end
	local ee = time();
	print(string.format(e, i, ee, ee - s));
end


Ran on an i5 core laptop with integrated graphics (HD 4000) and 8 GB ram

Thanks a million, this is something that has been bugging me in the back of my mind for quite a while when I’ve been doing optimizations.