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: math.deg(n) vs n/math.pi*180

Previous Thread :: Next Thread 
ArceusInator is not online. ArceusInator
Joined: 10 Oct 2009
Total Posts: 30553
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
Radioaktiivinen is not online. Radioaktiivinen
Joined: 25 Apr 2009
Total Posts: 18629
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
ArceusInator is not online. ArceusInator
Joined: 10 Oct 2009
Total Posts: 30553
06 Aug 2012 08:42 AM
Oh, I didn't even think about assigning math.deg to a variable.
Report Abuse
Tarabukka is not online. 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 is not online. 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
Brandonhare is not online. Brandonhare
Joined: 02 May 2007
Total Posts: 11005
06 Aug 2012 09:05 AM
Try it with i instead of 127. Also combine 180/math.pi into one number.
Report Abuse
aboy5643 is not online. 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
Techboy6601 is not online. Techboy6601
Joined: 29 Jun 2009
Total Posts: 4914
06 Aug 2012 02:52 PM
Is os.clock available in RBX.Lua?
Report Abuse
Dr01d3k4 is not online. 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
Techboy6601 is not online. Techboy6601
Joined: 29 Jun 2009
Total Posts: 4914
06 Aug 2012 02:54 PM
Hmm. Well, we can still use tick(), so it doesn't really matter.
Report Abuse
BlueTaslem is not online. BlueTaslem
Joined: 11 May 2008
Total Posts: 11060
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
Techboy6601 is not online. Techboy6601
Joined: 29 Jun 2009
Total Posts: 4914
06 Aug 2012 06:36 PM
It figures, because mulfast doesn't need to do extra calculations..
Report Abuse
BlueTaslem is not online. BlueTaslem
Joined: 11 May 2008
Total Posts: 11060
06 Aug 2012 06:59 PM
Well this shows empirically how much better it is.
Report Abuse
stravant is not online. stravant
Forum Moderator
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
trappingnoobs is not online. trappingnoobs
Joined: 05 Oct 2008
Total Posts: 19100
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 is not online. stravant
Forum Moderator
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
Techboy6601 is not online. Techboy6601
Joined: 29 Jun 2009
Total Posts: 4914
06 Aug 2012 07:21 PM
I'm sorry, but what exactly _is_ encapsulation?
Report Abuse
pwnedu46 is not online. 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 is not online. 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
jAlternate is not online. jAlternate
Joined: 02 Dec 2011
Total Posts: 234
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
ninjaknight101 is not online. ninjaknight101
Joined: 04 Apr 2011
Total Posts: 1281
06 Aug 2012 11:57 PM
Wow, you seem to know a lot about this guy...
Report Abuse
jAlternate is not online. jAlternate
Joined: 02 Dec 2011
Total Posts: 234
07 Aug 2012 12:05 AM
@ninjaknight101

I do. I've learnt all this information about him on his profile.
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