[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Speed of variables
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Speed of variables

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


Posted by tobiassjosten   Sweden  (79 posts)  [Biography] 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/
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] 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.


3,481 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]