TheMyrco
|
  |
| Joined: 13 Aug 2011 |
| Total Posts: 15105 |
|
|
| 29 Sep 2012 04:40 PM |
What, in general, are good 'rules' or 'statements' to write _efficient_ code (could be as ugly as hell). Even better would be if you could explain why.
Discuss (about efficiency, and on how to increase it as much as possiblos ;D). |
|
|
| Report Abuse |
|
|
|
| 29 Sep 2012 04:43 PM |
| Your baking a metaphorical cake , don't include any more steps then absolutely necessary for the quality of cake you desire. |
|
|
| Report Abuse |
|
|
TheMyrco
|
  |
| Joined: 13 Aug 2011 |
| Total Posts: 15105 |
|
|
| 29 Sep 2012 04:44 PM |
1) Always use local over global variables when possible. Local variables are destroyed immediatley when their scope ends.
2) for x = 1, #hax:GetChildren() do hax[x].Text = "nob" end
Is better than pairs, why? I think that v has to be calculated like done above (hax[x]) and then return it before being able to be used. Just a theory, tough.
3) Only use waits when really needed or to create a certain effect. Why? Just don't waste processing power.
----------------- Question; is there a difference with local function and 'plain' function? |
|
|
| Report Abuse |
|
|
TheMyrco
|
  |
| Joined: 13 Aug 2011 |
| Total Posts: 15105 |
|
|
| 29 Sep 2012 04:45 PM |
| @nick: If you aren't going to contribute anything useful, then please step trough the exit. |
|
|
| Report Abuse |
|
|
TheMyrco
|
  |
| Joined: 13 Aug 2011 |
| Total Posts: 15105 |
|
|
| 29 Sep 2012 04:47 PM |
| I wonder if semicolons, actually make a difference. I remember people saying that it is inefficient, but 'why?' would be a good question to pose in that situation... |
|
|
| Report Abuse |
|
|
|
| 29 Sep 2012 04:47 PM |
@Mycro Companies do not fire employees for not making enough sales before they meet there first client. Give me some time to draft up points.
What I was trying to say is that you should make your program as straightforward as possible.
Also , BindableEvents & BindableFunctions are lovely. |
|
|
| Report Abuse |
|
|
TheMyrco
|
  |
| Joined: 13 Aug 2011 |
| Total Posts: 15105 |
|
|
| 29 Sep 2012 04:49 PM |
@nick: "Also , BindableEvents & BindableFunctions are lovely." In what aspect? Efficiency? Usage? |
|
|
| Report Abuse |
|
|
TaslemGuy
|
  |
| Joined: 10 Jun 2009 |
| Total Posts: 12174 |
|
|
| 29 Sep 2012 05:22 PM |
1. Algorithms. Determine asymptopic cost.
Which is faster?
function fib(n) if n == 0 or n == 1 then return n end return fib(n-1) + fib(n-2) end
or
function fib(n) local a = 0 local b = 1 local i = 0 while i < n do i = i + 1 local x = a+b a = b b = a + b end return a end
They have time complexities O(2^n) and O(n) respectively to generate the nth fib number.
2. Operators and functions. Some are faster than others.
a.b is a slow operation. Who knows why. If you're using math.sqrt a lot, then do:
local sqrt = math.sqrt
And then use sqrt instead to avoid the . overhead. |
|
|
| Report Abuse |
|
|
LPGhatguy
|
  |
 |
| Joined: 27 Jun 2008 |
| Total Posts: 4725 |
|
|
| 29 Sep 2012 05:35 PM |
I'd like to know the efficiency difference of:
a = b and c or d
versus
if (b) then a = c or a = d end |
|
|
| Report Abuse |
|
|
TaslemGuy
|
  |
| Joined: 10 Jun 2009 |
| Total Posts: 12174 |
|
|
| 29 Sep 2012 05:44 PM |
@LPG
My benchmarks show they're roughly the same. My guess is that the difference is so small in comparison to other things (function call overhead, etc.) that there's no way you'd notice. |
|
|
| Report Abuse |
|
|
LPGhatguy
|
  |
 |
| Joined: 27 Jun 2008 |
| Total Posts: 4725 |
|
|
| 29 Sep 2012 05:47 PM |
@TaslemGuy But doesn't Lua have a special TESTSET opcode for this sort of thing?
You'd think that it'd be optimized to some degree. |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 29 Sep 2012 05:57 PM |
Let's talk about coding efficiency.
Mostly, my pet peeves.
1) Use self declaring variables/function names 2) Always use local variables, no exception 3) Never write anything more then once. If you're copying and pasting something, you're doing it wrong. 4) Tab your code so we can read it 5) Pick a coding style and use it. Use CamelCase, or SOMETHING. 6) Use variable/function names that are not acryonyms. No one else knows that FPSDDFKSLD means. 7) Declare all your variables at the top. This will help with keeping your variables straight. When I say top, I mean top of the scope. 8) Use comments for stuff you don't think you're remember.
That's my opinion. xD |
|
|
| Report Abuse |
|
|
|
| 29 Sep 2012 05:57 PM |
TESTSET opcode would be for which, the if statement or the and/or?
(and you used 'or' instead of else in your code...) |
|
|
| Report Abuse |
|
|
|
| 29 Sep 2012 05:58 PM |
Quenty: None of that has to do with code efficiency. The computer doesn't care how pretty or human readable your code is. |
|
|
| Report Abuse |
|
|
LPGhatguy
|
  |
 |
| Joined: 27 Jun 2008 |
| Total Posts: 4725 |
|
|
| 29 Sep 2012 06:02 PM |
@BlueTaslem lol, I did use 'or' instead of else.
I haven't coded since Ludum Dare.
TESTSET (unless I'm mistaken, I might be) is used for the and/or expression. |
|
|
| Report Abuse |
|
|
Legend26
|
  |
| Joined: 08 Sep 2008 |
| Total Posts: 10586 |
|
|
| 29 Sep 2012 06:13 PM |
"Also , BindableEvents & BindableFunctions are lovely."
I really don't like using roblox objects when we can just use _G although I guess they could help with client/server communication. |
|
|
| Report Abuse |
|
|
LPGhatguy
|
  |
 |
| Joined: 27 Jun 2008 |
| Total Posts: 4725 |
|
|
| 29 Sep 2012 06:34 PM |
@Legend26 BindableEvents and BindableFunctions aren't replicated (as of whenever they were introduced, at least.) |
|
|
| Report Abuse |
|
|
8SunTzu8
|
  |
| Joined: 30 Sep 2011 |
| Total Posts: 8199 |
|
|
| 29 Sep 2012 07:00 PM |
@Quenty,
"Never write anything more then once. If you're copying and pasting something, you're doing it wrong."
Say I have a GUI with 12 buttons that all do a "similar" thing. The functions would all be structured the same, however, each button will have an event when clicked to fire off a function to carry out this task.
Let's say it's as simple as printing something.
function A() print "A" end
function B() print "B" end
--etc...
script.Parent.ButtonA.MouseButton1Down:connect(A) script.Parent.ButtonB.MouseButton1Down:connect(B)
How would I instead combine these functions to use one function? (It would print A when I click ButtonA, and it would print B when I click ButtonB, etc...).
It would need to be as efficient as what I had shown, and work easily with 12 different buttons of a similar structure. I honestly don't think that re-writing code can reduce efficiency, or having 12 connections to similar functions that carry out slightly different tasks. It makes for a longer script, but at any given time, only smaller functions are being used. Maybe the listeners on the events might cause problems... But I don't know much about that.
"Contact me if you are interested in becoming a developer, innovator, or recruiter for CSA." |
|
|
| Report Abuse |
|
|
|
| 29 Sep 2012 07:23 PM |
^ Thats what I was about to ask. Also why hasn't anyone added anything about localscript against regular script?
I guess I will add that.
1. Always use local script whenever possible. It runs client side, reducing the amount of effort the server has to make. |
|
|
| Report Abuse |
|
|
Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 29 Sep 2012 07:36 PM |
| The point of high-level languages like Lua is you don't have to worry about all the little implementation details. If you are writing Lua code that couldn't be made more efficient, you're doing it wrong. If you are really interested in efficiency/optimization go learn C or C++ or assembly or something |
|
|
| Report Abuse |
|
|
|
| 29 Sep 2012 07:48 PM |
@8Sun I can't say it would be efficient, but I have come up with something that simplifies the writing process (sorta). Overall, it makes the code shorter :D
local buttons=script.Parent.Buttons:GetChildren() for i, v in pairs(buttons) do loadstring("script.Parent.Buttons."..v.Name..".MouseButton1Down:connect(function() print(string.match('"..v.Name.."', 'Button(.)')) end)")() end
I have tested this, and it works with any button that is named "Buttonsomething" and prints whatever comes after 'Button'. |
|
|
| Report Abuse |
|
|
Tenal
|
  |
| Joined: 15 May 2011 |
| Total Posts: 18684 |
|
|
| 29 Sep 2012 08:01 PM |
| Stop making your codes work 0.0000000000000001 seconds faster and actually make it work. |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 29 Sep 2012 08:04 PM |
Let's just put it this way...
I think coding your code efficiently (Faster) is better then worrying about game efficiency... |
|
|
| Report Abuse |
|
|
Garnished
|
  |
| Joined: 09 Apr 2012 |
| Total Posts: 12695 |
|
|
| 29 Sep 2012 08:27 PM |
| I am not efficient. I have no efficient code. I am an idiot. |
|
|
| Report Abuse |
|
|
TheMyrco
|
  |
| Joined: 13 Aug 2011 |
| Total Posts: 15105 |
|
|
| 29 Sep 2012 08:49 PM |
"Stop making your codes work 0.0000000000000001 seconds faster and actually make it work."
I am not, I am just interessted in this. |
|
|
| Report Abuse |
|
|