Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Speed of variables

Speed of variables

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by tobiassjosten   Sweden  (79 posts)  Bio
Date Mon 20 Jun 2005 08:00 PM (UTC)
Message
What's the difference in speed between these two examples?:
variable = false

.. and ..
if variable then variable = false end

I'm interested in numbers both when "variable" returns true and when it doesn't.

Simplicity is Divine | http://nogfx.org/
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Mon 20 Jun 2005 08:26 PM (UTC)
Message
variable = false is faster. Not by huge amounts, but it's faster.

I know that Lua doesn't translate directly into assembly, but here would be what MIPS assembly code would look like for both of those.

Assuming that 'variable' is in register $t0


variable = false
-->
move $t0, 0     # $t0 = false



if variable then variable = false end
-->
beq $t0, 0, DO_NOTHING     # if $t0 == 0, goto DO_NOTHING
move $t0, 0                # $t0 = false
DO_NOTHING:
...


The comparison instruction is generally speaking more costly than an assignment instruction. So, clearly, for a simple assignment of a boolean, variable=false is faster.

Explained differently, the comparison approach has to:
- load the current value
- compare it to something
- in some cases, store a new value

The direct assignment approach simply has to store a new value.

If you were really curious, you could run each version some 100,000 times and see which was faster. It is possible that Lua has some strangeness such that the first is faster on average but I doubt it.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


11,248 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.