Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
Haldrik
Wanderer


Joined: 03 Sep 2007
Posts: 88

PostPosted: Mon Apr 27, 2009 10:31 pm   

Lua vs Zscript? When to use which?
 
I failed to backup my CMUD and lost all my mud scripts... pretty big. So I was thinking I might just rewrite everything in LUA. Seems like it would be fun to learn how.

So now I've got a couple quick questions.


Is there any benefit to using LUA vs zscript for simple items? I.e. a highlights? My mud package is going to be fairly complex with lots of triggers and highlights etc.


Generally when I've been writing some complex stuff, its using lots of classes/aliases/ #T+ / #T- and LOTS of triggers. like 20+.

So basically, when should I try and use LUA? Zscript does a pretty good job of making some more complex scripts for me, but they are just sprawling and messy and If i have errors sometimes its hard to track down the issues.

Any tips or advice ?

Thanks!
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Mon Apr 27, 2009 11:14 pm   
 
Use Lua any time that you're doing something zScript can't do. Whether that means using a lua addon like LuaSQL, LuaSocket or whatever, or whether it simply means exploiting Lua's ability to have more complex data structures, or whatever you need it to do. If you don't need to use Lua for something, generally, don't. One line that's just zs.cw("blue") is bad. Twenty lines manipulating a complex table structure or parsing a webpage from LuaSocket is good.

Speed, whatever that means, is a highly subjective and very complex issue which I couldn't possibly give a full treatment to, but suffice it to say that when doing certain things in certain circumstances, you may find that Lua is faster than zScript. The specifics of which is faster for whatever situation will have to be discovered through experimentation.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Haldrik
Wanderer


Joined: 03 Sep 2007
Posts: 88

PostPosted: Tue Apr 28, 2009 12:03 am   
 
*g* It couldn't have been an easy answer.


Thanks fang :)
Reply with quote
wrym
Magician


Joined: 06 Jul 2007
Posts: 349
Location: The big palace, My own lil world

PostPosted: Wed Apr 29, 2009 11:13 pm   
 
I recently started writing a number of functions in lua to generate some statistics for my combat script. As part of this i wrote a few test scripts to compare lua vrs zscript speed, and also found a few cool tidbits of information.

Here is a portion of the code
Code:
<alias name="testszcriptitemlist" id="734">
  <value>#local $startt,$endt,$count
//use varible
$startt = @ostime()*1000 +%time("z")
#loop 1000 {
val = @val  +%item(@numbers,(%random(1,%numitems(@numbers))))
}
$endt = @ostime()*1000 +%time("z")
#print "val is" @val
#print "time for 1000 zscript &amp; varible rep:" %eval($endt-$startt)
#local $val


//use local &amp;cached item count
$startt = @ostime()*1000 +%time("z")
$count =%numitems(@numbers)
#loop 1000 {
$val = $val  +%item(@numbers,(%random(1,$count)))
}
$endt = @ostime()*1000 +%time("z")
#print "val is" $val
#print "time for 1000 zscript&amp;local rep:" %eval($endt-$startt)

</value>
</alias>
<alias name="testitemlist" language="Lua" id="733">
  <value><![CDATA[local startt, endt, val, string
   string = zs.var["numbers"]
   val = 0
   startt = luazs.timestamp()
   for i = 1,1000 do
   val = val + luazs.item(math.random(1,luazs.itemnum(string)),string)
   end -- end for loop
   endt = luazs.timestamp()
   print(" max is:"..val)
   print("Time for 1000 lua repetitions :"..endt-startt)
   val = 0
   startt = luazs.timestamp()
   for i = 1,1000 do
   val = val + zs.func.item(string , math.random(1,zs.func.numitems(string)))
   end -- end for loop
   endt = luazs.timestamp()
   print(" max is:"..val)
   print("Time for 1000 zs repetitions :"..endt-startt)]]></value>
</alias>
<event event="test" priority="7320" language="Lua" id="732">
  <value><![CDATA[print "loading lua std dev and other functions"
function split(str, pat)
   local t = {}  -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
    table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end -- end while
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end -- end if
   return t
end -- end function

if (not (luazs)) then luazs = {} end

luazs.item = function(item,list)
if type(list) == "table" then
 return list[item]
else
 if luazs.itemnum(list) < item then
 return nil
 else
 return split(list, "|")[item]
 end -- end if count
 end -- end table if
end -- end item

luazs.itemnum = function(list)
if type(list) == "table" then
return table.maxn(list)
else
local _, num
_,num = string.gsub(list,"|","")
return num
end --end if type table
end -- end itemnum

luazs.timestamp = function()
  return(os.time()*1000+zs.time("z"))
end

   ]]></value>
</event>
<var name="numbers" type="StringList" id="731">1|4|6|12|3|64|12|32|15|23|23|14|16|23|46|57|24|3|67|23|45|23|56|23|57|13|24|24|35|46|68|43|32|53|32|21</var>
<var name="val" type="Integer" id="735">0</var>


The first thing I found was, cmud converts stringlists into hashtables for lua, and consequently, stringlists, and literal strings make a big difference, for both zscript and lua.

with numbers as a literal string:
Code:
 max is:30885
Time for 1000 lua repetitions :67
 max is:29829
Time for 1000 zs repetitions :218
val is 2657544
time for 1000 zscript & varible rep: 138
val is
time for 1000 zscript&local rep: 47


with numbers as a string list:
Code:
 max is:29860
Time for 1000 lua repetitions :7
 max is:29598
Time for 1000 zs repetitions :299

val is 2953369
time for 1000 zscript & varible rep: 104
val is
time for 1000 zscript&local rep: 30


Now this is only from a single run but the numbers held pretty stable
for 1000 repetitions straight lua was fastest followed relatively closely by local variable zscript.
The worse case was lua repeatedly ascessing zscript functions on a string list some 40x time slower.

Zscript == fast
lua == very fast
jumping between the too == really slow

Hmmm, i'ld say hope this makes it easier... but I know it didn't, nor simpler. Hard test like this are really about only way to find out for sure.
Reply with quote
wrym
Magician


Joined: 06 Jul 2007
Posts: 349
Location: The big palace, My own lil world

PostPosted: Thu Apr 30, 2009 2:06 am   
 
Ok now i'm confused...

I realized i had forgotten one test, calling the lua implemetnation of %item from zscript... so i made a wrapper function for luazs.item and ran this code

Code:
$val = 0
$startt = @ostime()*1000 +%time("z")
$count = %numitems(@numbers)
#loop 1000 {
$val = $val + @luaitemnum(@numbers,(%random(1,$count)))
}
$endt = @ostime()*1000 +%time("z")
#print "val is" $val
#print "time for 1000 zscript calling lua rep:" ($endt-$startt)


Which consistently gives me 780 or so, and here i was expecting something between the pure lua/pure zscript times..... BUT @numbers being a string list, or a literal string makes NO difference... next test

Code:
<func name="test" language="Lua" id="737">
  <value>return(type(zs.param(1)))</value>
</func>


#print @test(@numbers)

With numbers as a literal and as a string list both return a table, so somewhere there.... it's getting converted to a hash table?

mkay, hopefully I don't figure anything else to muddy the waters
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Thu Apr 30, 2009 11:55 am   
 
wrym wrote:
Zscript == fast
lua == very fast
jumping between the too == really slow

I'm afraid I don't have time to read that wall of text now, but this is my assumption. I'd also wonder when exactly the Lua code in CMUD settings is compiled (Lua uses a JIT compiler much like zScript does) and if that compiled code is stored anywhere, or if it's compiled every time. If it isn't stored then putting your Lua code into function...end (which will be stored) and putting as little actual code into a script as possible (to speed up the repeat compilations) will be advantageous.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net