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: Optomization vs. Common Sense

Previous Thread :: Next Thread 
ShiroPixel is not online. ShiroPixel
Joined: 31 Jul 2010
Total Posts: 545
17 Mar 2011 06:38 PM
Before I explain what I mean, I must show you why I mean that.
_______________________
function Name()
local squareRoot = math.sqrt(100);
print(squareRoot);
end;
_______________________

Now, that runs 30% faster then:
_______________________
function Name()
print(math.sqrt(100);
end;
_______________________

My question is why?
When you are indexing a function for the local variable.. shouldn't that be SLOWER?
You are trying to index a local variable, and the local variable is slightly slower when being called, a couple miliseconds.

Now, wouldn't it make sense if you called the function instead of setting a variable to call the function instead? Instead of going through the extra work of calling the variable and setting a function for it, just skip a step and call the function directly.
Makes no sense, versus common sense.
Report Abuse
myrkos is not online. myrkos
Joined: 06 Sep 2010
Total Posts: 8072
17 Mar 2011 06:39 PM
Are you sure about that?
Report Abuse
ShiroPixel is not online. ShiroPixel
Joined: 31 Jul 2010
Total Posts: 545
17 Mar 2011 06:40 PM
Surprisingly yes, it does run 30% faster.
Report Abuse
myrkos is not online. myrkos
Joined: 06 Sep 2010
Total Posts: 8072
17 Mar 2011 06:41 PM
Can you possibly post the code you used to time it?
Report Abuse
ShiroPixel is not online. ShiroPixel
Joined: 31 Jul 2010
Total Posts: 545
17 Mar 2011 06:42 PM
According to the LUA.ORG documentations, yes it does run faster.
Report Abuse
myrkos is not online. myrkos
Joined: 06 Sep 2010
Total Posts: 8072
17 Mar 2011 06:44 PM
I would think the first would run faster only if you were to print it several times.
Report Abuse
Emess is not online. Emess
Joined: 01 Apr 2010
Total Posts: 13331
17 Mar 2011 06:47 PM
I doubt the first example is faster. Are you sure it runs faster?
Report Abuse
ShiroPixel is not online. ShiroPixel
Joined: 31 Jul 2010
Total Posts: 545
17 Mar 2011 06:47 PM
"places where you can use locals besides the obvious ones. For instance, if you
call a function within a long loop, you can assign the function to a local variable.
For instance, the code


for i = 1, 1000000 do
local x = math.sin(i)
end


runs 30% slower than this one:


local sin = math.sin
for i = 1, 1000000 do
local x = sin(i)
end"

Excerpt from lua.org

"So, it is easy to justify one of the most important rules to improve the performance
of Lua programs: use locals!"

Report Abuse
myrkos is not online. myrkos
Joined: 06 Sep 2010
Total Posts: 8072
17 Mar 2011 06:49 PM
Well, that's different.
Report Abuse
ShiroPixel is not online. ShiroPixel
Joined: 31 Jul 2010
Total Posts: 545
17 Mar 2011 06:50 PM
Of course it is different. I'm just using my example, not copying theirs.
Report Abuse
myrkos is not online. myrkos
Joined: 06 Sep 2010
Total Posts: 8072
17 Mar 2011 06:56 PM
"function Name()
local squareRoot = math.sqrt(100);
print(squareRoot);
end; "

Here, you're declaring the variable and making the result of "math.sqrt(100)" its value.

"local sin = math.sin
for i = 1, 1000000 do
local x = sin(i)
end"

Here, you're making a global function local.
Report Abuse
TheSquirrel is not online. TheSquirrel
Joined: 23 Aug 2008
Total Posts: 5557
17 Mar 2011 07:13 PM
So if you were to do this:

local sqrt = math.sqrt

function Name()
print(sqrt(100))
end

-------------------------------------------
It would be faster than:
-------------------------------------------
function Name()
print(math.sqrt(100))
end
Report Abuse
mattchewy is not online. mattchewy
Joined: 19 Feb 2008
Total Posts: 7300
17 Mar 2011 07:14 PM
"Now, that runs 30% faster then:
_______________________
function Name()
print(math.sqrt(100);
end;"

That is probably because the second function has a syntax error...
Report Abuse
chrislo27 is not online. chrislo27
Joined: 13 Jun 2009
Total Posts: 1776
17 Mar 2011 07:15 PM
@matt

OH YEAH!! (missing a parentheses)
Report Abuse
XlegoX is not online. XlegoX
Joined: 16 Jun 2008
Total Posts: 14955
17 Mar 2011 08:01 PM
First example: I suspect there's a problem with your profiling metric. There is actually a difference in the way the two are compiled, however they only differ by a single move instruction, which is completely irrelevant considering you're calling sqrt, which is a very expensive call compared to cheap opcodes. (the time spent in the sqrt code is probably at least equal to the time spent doing the rest of the "work" in your test, probably even more)


for i = 1, 1000000 do
local x = math.sin(i)
end

VS

local sin = math.sin
for i = 1, 1000000 do
local x = sin(i)
end"

This case is actually very clear cut, there's a good reason you see a big speed discrepancy.

Effectively, when you do this: "math.sin(a)" Lua has to do this:

-Get the current global table
-Query the global table for an object keyed with "math", store it in local #1
-Query local #1 for something called with "sin", store it in local#2
-Call local#2 with 1 argument

VS the second case where the function is already stored in a local:

-Call local #1 with 1 argument (only a single instruction in Lua's op-code format)

It's clear why there is a speed difference
Report Abuse
Merlin11188 is not online. Merlin11188
Joined: 20 Dec 2007
Total Posts: 4158
17 Mar 2011 09:50 PM
So, which would be faster?

local log=math.log10()
local sqrt=function(x) y=10^((log(x)/2) return y end

for i=1,1000 do
print(sqrt(4))
end

or

for i=1,1000 do
print(math.sqrt(4))
end
Report Abuse
XlegoX is not online. XlegoX
Joined: 16 Jun 2008
Total Posts: 14955
17 Mar 2011 10:23 PM
math.log is just a slow.

The best case is to avoid using square roots and other fancy ops altogether and only use addition and multiplication where possible. Modern processor are good at doing those ops very very fast with dedicated opcodes, unlike square roots and logs etc which usually require many opcodes and 100's of cycles to run.
Report Abuse
crazypotato4 is not online. crazypotato4
Joined: 22 Feb 2010
Total Posts: 20000
17 Mar 2011 11:42 PM
Even if math.log is slow, merlin's first script would go faster, 'cause it'd error on the first repetition :P

Attempt to call local 'log' (a nil value)

amirite ^
Report Abuse
MrBlockson is not online. MrBlockson
Joined: 31 Aug 2008
Total Posts: 14276
18 Mar 2011 02:54 AM
"faster then:"

What is it with Americans and the inability to differentiate between THEN and THAN.

My God......
Report Abuse
Catblox is not online. Catblox
Joined: 23 Apr 2008
Total Posts: 2223
18 Mar 2011 10:56 AM
Than you better get used to it. It's better then other mistakes people make, and than again it may not be.
Report Abuse
nightname is not online. nightname
Joined: 10 Jun 2008
Total Posts: 8960
18 Mar 2011 11:39 AM
"It's better then other mistakes people make, and than again it may not be."

Oh my...
Didn't MrBlockson just point out how people cannot differentiate between THEN and THAN? Now you come along and do what will make you look like an idiot?

I don't know if you did that on purpose, but why? Just why?
Report Abuse
Emess is not online. Emess
Joined: 01 Apr 2010
Total Posts: 13331
18 Mar 2011 12:10 PM
I doubt it's nice to stereotype Americans by saying that they mix then and than together...
Report Abuse
fliboys is not online. fliboys
Joined: 26 Mar 2010
Total Posts: 5559
18 Mar 2011 12:17 PM
Us Americans are AMERICAN.

We can do things like that. :D
Report Abuse
Merlin11188 is not online. Merlin11188
Joined: 20 Dec 2007
Total Posts: 4158
18 Mar 2011 12:59 PM
I never mix up then and than. Silly other Americans giving me a bad reputation.

@crazy, yes it does. It also errors the next line because I forgot a parenthesis.
Report Abuse
mattchewy is not online. mattchewy
Joined: 19 Feb 2008
Total Posts: 7300
18 Mar 2011 03:32 PM
It's not Americans. It's called being illiterate.
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