|
| 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
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
| |
|
|
| 17 Mar 2011 06:40 PM |
| Surprisingly yes, it does run 30% faster. |
|
|
| Report Abuse |
|
|
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 |
|
|
|
| 17 Mar 2011 06:42 PM |
| According to the LUA.ORG documentations, yes it does run faster. |
|
|
| Report Abuse |
|
|
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
|
  |
| 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 |
|
|
|
| 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
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
| |
|
|
| 17 Mar 2011 06:50 PM |
| Of course it is different. I'm just using my example, not copying theirs. |
|
|
| Report Abuse |
|
|
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 |
|
|
|
| 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
|
  |
| 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
|
  |
| Joined: 13 Jun 2009 |
| Total Posts: 1776 |
|
|
| 17 Mar 2011 07:15 PM |
@matt
OH YEAH!! (missing a parentheses) |
|
|
| Report Abuse |
|
|
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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 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 |
|
|
|
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| Joined: 19 Feb 2008 |
| Total Posts: 7300 |
|
|
| 18 Mar 2011 03:32 PM |
| It's not Americans. It's called being illiterate. |
|
|
| Report Abuse |
|
|