|
| 06 Aug 2012 08:34 AM |
Which is more efficient?
I'd think math.deg is less efficient, seeing as you have to call a function. |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 08:39 AM |
local todeg=math.deg
todeg(a)
is probably fastest as its a single operation while the other one is liek 3 operations.
i bet math.deg is c side math... |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 08:42 AM |
| Oh, I didn't even think about assigning math.deg to a variable. |
|
|
| Report Abuse |
|
|
Tarabukka
|
  |
| Joined: 18 Jan 2011 |
| Total Posts: 394 |
|
|
| 06 Aug 2012 08:51 AM |
math.deg is likely to be more efficient since it's written in C:
static int math_deg (lua_State *L) { lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); return 1; } |
|
|
| Report Abuse |
|
|
Tarabukka
|
  |
| Joined: 18 Jan 2011 |
| Total Posts: 394 |
|
|
| 06 Aug 2012 08:58 AM |
Wow, that's funny, just ran a quick benchmark:
Testing math.deg(127)
elapsed time: 1.26
Testing 127/math.pi*180
elapsed time: 0.86
Benchmarking code (stolen from somewhere):
local x = os.clock() local s = 0 print("Testing math.deg(127)\n") for i=1,10000000 do math.deg(127) end print(string.format("elapsed time: %.2f\n", os.clock() - x))
print("Testing 127/math.pi*180\n") local x = os.clock() local s = 0 for i=1,10000000 do local v = 127/math.pi*180 end print(string.format("elapsed time: %.2f\n", os.clock() - x)) |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 09:05 AM |
| Try it with i instead of 127. Also combine 180/math.pi into one number. |
|
|
| Report Abuse |
|
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 06 Aug 2012 12:04 PM |
| I don't know who this Tarabukka person is, but I respect him and allow him to post whatever he wants here based on his last few posts :D |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 02:52 PM |
| Is os.clock available in RBX.Lua? |
|
|
| Report Abuse |
|
|
Dr01d3k4
|
  |
| Joined: 11 Oct 2007 |
| Total Posts: 17916 |
|
|
| 06 Aug 2012 02:53 PM |
| @Tech: I don't think so; the whole os library was removed from Roblox, I think? |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 02:54 PM |
| Hmm. Well, we can still use tick(), so it doesn't really matter. |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 03:09 PM |
function std(a) return math.deg(a) end
function mul(a) return a / math.pi * 180 end
function mulfast(a) return a * 57.2958 end
local kk = math.deg function stdfast(a) return kk(a) end
function test(fn,str,rep) local t = os.clock() local i = 0 while i < rep do i = i + 1 fn(math.random()*6.28) end print(str .. " took " .. (os.clock() - t) .. " seconds.") end
test(std,"std",1000000) test(mul,"mul",1000000) test(stdfast,"stdfast",1000000) test(mulfast,"mulfast",1000000)
std took 0.776 seconds. mul took 0.58 seconds. stdfast took 0.588 seconds. mulfast took 0.454 seconds. |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 06:36 PM |
| It figures, because mulfast doesn't need to do extra calculations.. |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 06:59 PM |
| Well this shows empirically how much better it is. |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 06 Aug 2012 07:07 PM |
Anything that takes less function calls will be cheaper unless the C-side code is doing a _lot_ of work for you.
The cost of a function call, especially one to C-side code, is really large compared with simpler opcodes such as adding and multiplying. |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 07:12 PM |
| Semirelevant, is there any significant performance loss using encapsulation functions over native encapsulation or directly adjusting variables? |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 06 Aug 2012 07:18 PM |
Any time you encapsulate anything in Lua there's a performance cost. The Lua compiler does 0 optimization, so if you want something to be fast it should be in the simplest term possible.
That said, most of the time on Roblox you shouldn't even worry about that, since the cost of calls to the Roblox API will dwarf the cost of any actual Lua data manipulation you're doing unless it's something really expensive like pathfinding. |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 07:21 PM |
| I'm sorry, but what exactly _is_ encapsulation? |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 06 Aug 2012 11:19 PM |
| @techboy: Encapsulation is enclosing objects in a common interface in a way that makes them interchangeable, and guards their states from invalid changes (From wikipedia) |
|
|
| Report Abuse |
|
|
Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 06 Aug 2012 11:22 PM |
> most of the time on Roblox you shouldn't even worry about that
I agree. The beauty of scripting on ROBLOX is that you shouldn't be concerned about optimization. You should just write efficient scripts and not bother to optimize them unless they really need it. |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 11:40 PM |
"I don't know who this Tarabukka person is"
He is that guy who runs BCy. He doesn't actually play this game. He mainly codes in PHP as a web developer, using MySQL, HTML, CSS and JavaScript as well. Nginx and PHP-FPM is his favourite combination! He is currently learning C++ and working on his own Kohana-like PHP framework. He has also recently done some low-level stuff in C (writing his own basic x86 kernel). |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2012 11:57 PM |
| Wow, you seem to know a lot about this guy... |
|
|
| Report Abuse |
|
|
|
| 07 Aug 2012 12:05 AM |
@ninjaknight101
I do. I've learnt all this information about him on his profile. |
|
|
| Report Abuse |
|
|