generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Should I put local before function or not?

Previous Thread :: Next Thread 
kinlee01 is not online. kinlee01
Joined: 03 Nov 2012
Total Posts: 75
23 Sep 2015 04:20 AM
local function ()
end

==================

function ()
end
Report Abuse
AmbientOcclusion is online. AmbientOcclusion
Joined: 10 Feb 2009
Total Posts: 1323
23 Sep 2015 04:24 AM
Local makes a function or variable only limited to a scope. it is usually good to use it when you don't want the function anywhere else.

do
function func()
print'ellu'
end
func() -- runs fine
end

func() --error
Report Abuse
distalDigitPhalanx is not online. distalDigitPhalanx
Joined: 25 Jan 2015
Total Posts: 1509
23 Sep 2015 07:18 AM
www.lua.org/gems/sample.pdf

Read from page 17.
Or read all of it. Your call.
Report Abuse
Stymi is not online. Stymi
Joined: 19 Sep 2015
Total Posts: 1077
23 Sep 2015 07:27 AM
Always use the local keyword if you're defining the function in a code block, but otherwise it's not going to help you at all. Do so if you wish to.

inb4 some scripter who thinks they're the crap starts flaming because there's a 0.0000000000000000000002 second performance benefit
Report Abuse
distalDigitPhalanx is not online. distalDigitPhalanx
Joined: 25 Jan 2015
Total Posts: 1509
23 Sep 2015 07:29 AM
I would like my 0.0000000000000000000002 seconds back
Report Abuse
Stymi is not online. Stymi
Joined: 19 Sep 2015
Total Posts: 1077
23 Sep 2015 07:34 AM
But is 0.000000000000000002 seconds worth it if you spend 0.75 seconds typing it? Probably not
Report Abuse
distalDigitPhalanx is not online. distalDigitPhalanx
Joined: 25 Jan 2015
Total Posts: 1509
23 Sep 2015 07:34 AM
Woah. Looks like someones' got a degree in applied economics. Is that even a thing?
Report Abuse
Stymi is not online. Stymi
Joined: 19 Sep 2015
Total Posts: 1077
23 Sep 2015 07:35 AM
According to Google, yep.
Report Abuse
distalDigitPhalanx is not online. distalDigitPhalanx
Joined: 25 Jan 2015
Total Posts: 1509
23 Sep 2015 07:38 AM
Swag. Looks like SOMEONE (me) has got a degree in applied linguistics
Report Abuse
Stymi is not online. Stymi
Joined: 19 Sep 2015
Total Posts: 1077
23 Sep 2015 07:39 AM
And that is ALSO a thing.

Versus....theoretical linguistics???
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
23 Sep 2015 10:08 AM
Actually, you should probably use it.
Not only is it a good practice that is pretty much how every programming language except Lua works, but it also increases performance SIGNIFICANTLY, especially if you have a loop or are accessing variables (Such as the function itself) quickly.
Report Abuse
Valenthian is not online. Valenthian
Joined: 01 Oct 2013
Total Posts: 590
23 Sep 2015 10:12 AM
Roblox staff who makes catalog gear doesn't use it.
sloppy af
and they capitalize Workspace
Report Abuse
distalDigitPhalanx is not online. distalDigitPhalanx
Joined: 25 Jan 2015
Total Posts: 1509
23 Sep 2015 10:28 AM
Roblox gear (is in my) socks
Report Abuse
kinlee01 is not online. kinlee01
Joined: 03 Nov 2012
Total Posts: 75
23 Sep 2015 12:16 PM
Thanks for the Replies guys.
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
23 Sep 2015 12:24 PM
The performance benefit of using local variables is indeed much greater than "0.000000000000000002".


Muad'Dib
Report Abuse
Stymi is not online. Stymi
Joined: 19 Sep 2015
Total Posts: 1077
23 Sep 2015 12:33 PM
No. No it isn't.

Your local variables aren't going to save you.
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
23 Sep 2015 12:47 PM
Oh really? I kinda just did some benchmarks and a simple script that just adds a variable to itself (like this: a = 4; a = a + a) inside a loop and using local variables make it run ~20 seconds faster.


Muad'Dib
Report Abuse
direMitten is not online. direMitten
Joined: 18 Jun 2015
Total Posts: 1294
23 Sep 2015 12:49 PM
Will it ever even terminate? <.< As far as I can see, it's an infinite loop
Report Abuse
Stymi is not online. Stymi
Joined: 19 Sep 2015
Total Posts: 1077
23 Sep 2015 12:52 PM
> local t1 = tick() local x = 0 for i = 1, 1000000 do x = x + 1 end local t2 = tick() print(t2 - t1)
0.015406608581543
> local t1 = tick() x = 0 for i = 1, 1000000 do x = x + 1 end local t2 = tick() print(t2 - t1)
0.08324408531189

Sure, go ahead and use local variables- I always do! But it's not something that really matters all that much.
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
23 Sep 2015 12:52 PM
If it had never terminated how would I have known how long it took from start to termination?


Muad'Dib
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
23 Sep 2015 01:01 PM
I re-did my test this time looping a bit higher and the time saved from local variables became ~30 seconds.

---
do
local start = tick()
local n = 0.1
for i = 1,25000000 do
n = n + n
end
print(tick() - start) --> 53.432
end
---
do
local start = tick()
n = 0.1
for i = 1,25000000 do
n = n + n
end
print(tick() - start) --> 83.347
end
---


Muad'Dib
Report Abuse
Stymi is not online. Stymi
Joined: 19 Sep 2015
Total Posts: 1077
23 Sep 2015 01:02 PM
"for i = 1,25000000 do"

25,000,000

wow 30 seconds on 25 MILLION iteratons big difference
Report Abuse
direMitten is not online. direMitten
Joined: 18 Jun 2015
Total Posts: 1294
23 Sep 2015 01:22 PM
25m is a lot, but not unthinkable. But yeah, in the scenarios where 25m iterations of anything ever is required, 30 secs ain't alot.
Report Abuse
Luxurize is not online. Luxurize
Joined: 31 Mar 2015
Total Posts: 1289
23 Sep 2015 01:25 PM
'local' before a function is only good if you're only going to call that function once.


“Insane isn't always black or white. We're all pathological, in our own ways.”
Report Abuse
Stymi is not online. Stymi
Joined: 19 Sep 2015
Total Posts: 1077
23 Sep 2015 01:27 PM
"'local' before a function is only good if you're only going to call that function once."

What? No... That's just completely wrong.

And yeah, they're not necessary. 30 seconds on 2.5m iterations as the other guy said, is not going to make a very big difference.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image