Variables

Posted by MUSHuser on Fri 07 Jan 2011 08:10 AM — 4 posts, 15,770 views.

#0
This might be a silly question. I am just wondering about the speed of storing and retrieving a variable via two options.

1. Through the script. For example: Send("Text " + Variable)
2. Through the client. For example: Text @Variable

My guess is that it is faster to store using the script but faster to retrieve from the client. Thanks in advance for any replies.
Australia Forum Administrator #1
It's negligible either way. Consider this:


start = utils.timer ()

local x

for i = 1, 1000000 do
  x = GetVariable ("target")
end -- for

print ("time taken =", utils.timer () - start)  


Result was:


time taken = 1.0009219048079


In other words, it got one million variables in a second.

In a normal MUD situation, you might get or set 10 or 20 variables? So, the time taken is insignificant.

And look at this:


start = utils.timer ()

for i = 1, 1000000 do
  SetVariable ("foo", "bar")
end -- for

print ("time taken =", utils.timer () - start)  


Result was:


time taken = 0.80276772542857


So it set a million variables in 8/10 of a second.
Amended on Fri 07 Jan 2011 08:30 AM by Nick Gammon
#2
The GetVariable and SetVariable functions would be through the client... is that the same as using @Variable to get a variable? How does this compare with getting and setting the variables through the script?
Australia Forum Administrator #3
Well, both are done "through the client" - one uses the scripting engine, one is done by text substitution in the "send" box.

I suppose that @variable might be slightly faster if you weren't doing scripting for any other purpose. If you are already sending to script then it is using the script engine anyway. Also, if you check "expand variables" that is an extra step the client has to do (to look for @variable) which it doesn't otherwise do.

As I said, the difference would be negligible. You may as well worry whether long or short variable names would be faster. I would use whatever is easiest under the circumstances, and which is easiest to read and maintain.