Functions in modules - Which is more efficient?

Suppose I have 3 levels in my game. I have included them in two different ways. I can’t decide on which one to use.

The first
[lua]–levels.lua
local level1 = function() … end
local level2 = function() … end
local level3 = function() … end
return level1,level2,level3

–main.lua
local l1,l2,l3
l1,l2,l3 = require “levels”
l1();
l2();
l3();[/lua]

and the second
[lua]–level1.lua
local level1 = function() … end
return level1

–level2.lua
local level2 = function() … end
return level2

–level3.lua
local level3 = function() … end
return level3

–main.lua
local l1=require “level1”
local l2=require “level2”
local l3=require “level3”
l1();
l2();
l3()[/lua]

When running the game, I felt the first one was a bit slower. Particularly when executing the functions… i.e l1(); or l2();
I’m not sure though. When the no. levels increase, I guess the performance will decrease.

So anybody have any idea to which of the above two will be more effiecient with respect to game performance??
Any help is appreciated… Thanks

P.S. I have a number of levels and in each level function, I create a number of display objects
[import]uid: 64174 topic_id: 16590 reply_id: 316590[/import]

It’s really a matter of personal preference, but I would recommend the second option as your levels.lua could get pretty ugly depending on the size of your levels. [import]uid: 52430 topic_id: 16590 reply_id: 61976[/import]

Actually I prefer the first cos that makes my code more compact and easily accessible :slight_smile:

One question though… In the first method, When I call l1() are l2 and l3 also compiled(or whatever…Forgive my terminology.I am no Lua expert)?? And when I call l2() are l1 and l3 compiled??
If that is the case, that make the first method less efficient,doesn’t it? Hope that makes sense…

BTW Jon, Thanks for this
http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/
Really helped a lot! :slight_smile: [import]uid: 64174 topic_id: 16590 reply_id: 61981[/import]