|
| 06 Aug 2011 08:36 PM |
I made a converter of fahrenheit to celsius, Its there:
function convert(fahrenheit) a = fahrenheit - 32 print(a / 1.8) end
convert(70) |
|
|
| Report Abuse |
|
|
MyrcoAlt
|
  |
| Joined: 31 Mar 2011 |
| Total Posts: 209 |
|
| |
|
|
| 06 Aug 2011 08:49 PM |
More efficient:
local function convert(fahrenheit) local a = fahrenheit - 32 return a / 1.8 end
print(convert(70))
Even more efficient:
local function convert(fahrenheit) return (fahrenheit - 32) / 1.8 end |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2011 09:25 PM |
| Pokelover, I doubt your second example is more efficient when measuring runtime. |
|
|
| Report Abuse |
|
|
blocco
|
  |
| Joined: 14 Aug 2008 |
| Total Posts: 29474 |
|
|
| 06 Aug 2011 09:27 PM |
| @Necro: It should be, because the interpreter doesn't have to take the time to set the variable "a". |
|
|
| Report Abuse |
|
|
jode6543
|
  |
| Joined: 16 Jun 2009 |
| Total Posts: 5363 |
|
|
| 06 Aug 2011 09:43 PM |
@Blocco I think the point he was getting at that the change is so small it has almost no effect.
-Jode |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2011 09:50 PM |
@Necro:
local function convert1(fahrenheit) local a = fahrenheit - 32 return a / 1.8 end
local function convert2(fahrenheit) return (fahrenheit - 32) / 1.8 end
local t = os.clock() for i = 1, 10000000 do convert1(70) end print(os.clock() - t) local t = os.clock() for i = 1, 10000000 do convert2(70) end print(os.clock() - t)
Using the regular Lua VM (too lazy to test LuaJIT), the second one is faster by ~0.011s on average doing that many iterations. Sure, it's not a lot, and no one would realistically be doing that many iterations for any normal reason, but it's still faster. |
|
|
| Report Abuse |
|
|
Aaaboy97
|
  |
| Joined: 05 Apr 2009 |
| Total Posts: 6612 |
|
|
| 06 Aug 2011 09:51 PM |
If you're going for better speed, remember that multiplication is faster than division, but if you use multiplication, you're going to have to lose some accuracy.
local c = (f - 32)*0.55555555555556 |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2011 09:58 PM |
| @Aaaboy: In my opinion, do things as fast as possible while doing it as accurate as possible. Though multiplication does yield a bit faster, it's ~0.05 seconds faster than each of the previous tests I posted if you replace the division with multiplication. |
|
|
| Report Abuse |
|
|
|
| 07 Aug 2011 09:56 AM |
| The only real difference between them being the local keyword, the VM would have to keep track of that register as being local, and the second example might use one less register, but who knew that would have an 11 millisecond delay o_O. |
|
|
| Report Abuse |
|
|