Speed of scripts

Post Reply
Delrayne
Posts: 159
Joined: Tue Jun 07, 2011 7:07 pm
Contact:

Speed of scripts

Post by Delrayne »

I've been tinkering with my healing functions etc... and I've devised a way to have one function check against my priorities and cure accordingly. Previously I had a function for each cure type(herb, salve, smoke, etc..) that would check it's affliction priority and cure accordingly. I would then place calls to these multiple curing functions in a single function that would get called on my prompt.

What I've changed it to is one function that loops through a table to determine cure order(herb, salve, smoke, etc..) that then loops through another table to check for affliction order. Then it heals accordingly. This method effectively saves me about 200 or more lines of code in various function and table construction.

I'm typing this on my phone else I would provide code for better explanation. Basically what I want to know is which method is faster as well as tips, sites, or even code I could use to test the speed of certain scripts. As always thanks in advance for any help, tips, or advice.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: Speed of scripts

Post by tsuujin »

I've read through this a couple of times, and I honestly don't know what you're asking here. You've asked which method is faster, but I don't know what two methods you're comparing. Are you asking if it's faster to loop through tables or to hard code it?

Delrayne
Posts: 159
Joined: Tue Jun 07, 2011 7:07 pm
Contact:

Re: Speed of scripts

Post by Delrayne »

Both scenarios loop through tables. One just uses multiple functions called in a single function and the other is just one function. I know it's hard to picture without code, and my programming grammar may be off, but in about 8 hours I can give you guys some code to look at.

Delrayne
Posts: 159
Joined: Tue Jun 07, 2011 7:07 pm
Contact:

Re: Speed of scripts

Post by Delrayne »

Alright so here is some code to help explain the two different methods.

This is the new method:
Code: [show] | [select all] lua
Healing = {
	"herb",
	"salve",
	"smoke",
	"purge",
	"focus",
	"tree",
	"writhe",
	Priority = {
		herb = {
			{name = "stupidity", cure = "orphine", timer = createStopWatch()},
			{name = "hemotoxin", cure = "kelp", timer = createStopWatch()},
                        --and so on for all herb afflictions
		},
		salve = {
			{name = "concussion", cure = "restoration to head", timer = createStopWatch()},
			{name = "anorexia" , cure = "epidermal to torso", timer = createStopWatch()},
                        --and so on for all salve afflictions
		},
		smoke = {
			{name = "aeon" , cure = "laurel", timer = createStopWatch()},
			{name = "madness" , cure = "laurel", timer = createStopWatch()},
                         --and so on for all smoke afflictions
		},
		purge = {
			{name = "stupidity", cure = "purge blood", timer = createStopWatch()},
			{name = "paralysis", cure = "purge blood", timer = createStopWatch()},
                        --and so on for all purge afflictions
		},
		focus = {
			{name = "stupidity", cure = "focus mind", timer = createStopWatch()},
			{name = "anorexia", cure = "focus mind", timer = createStopWatch()},
                        --and so on for all focus afflictions
		},
		tree = {
			{name = "stupidity", cure = "touch tree", timer = createStopWatch()},
			{name = "hemotoxin", cure = "touch tree", timer = createStopWatch()},
                        --and so on for all tree afflictions
		},
		writhe = {
			{name = "impaled", cure = "impale", timer = createStopWatch()},
			{name = "transfixed", cure = "transfix", timer = createStopWatch()},
                        --and so on for all writhe afflictions
		},
	}
}

function cureMe(heal,aff,cure)
	if (heal == "herb" and ableToEat() and cureCheck(aff)) then
		balance.herb = false
		try.herb = aff
		sendAll("outr 1 " .. cure,"eat " .. cure)
	elseif (heal == "salve" and ableToApply() and cureCheck(aff)) then
		balance.salve = false
		try.salve = aff
		send("apply " .. cure) 
        --for all healing types (herb, salve, smoke, purge, focus, tree, writhe)
	end
end

function healAll() --Runs on every prompt
	for k, cure in ipairs (Healing) do --[[determines which methods of curing to do first, as defined in the 'Healing' table]]
		for i, aff in ipairs (Healing.Priority[cure]) do  --Loops through every affliction by cure type
			if (user.affs[aff.name]) then  --compares it to 'user.affs' which is either true or nil(doesn't exist)
				cureMe(cure,aff.name,aff.cure)   --cures the affliction
			end
		end
	end
end
This is the old method:
Code: [show] | [select all] lua
--for the sake of space lets all assume that 'herbPriority' is the same as 'Healing.Priority.herb' from the above table

function herbCuring()
--Can't eat more if we just ate, or can't eat food.
	if (unableToEat()) then
		return
	end -- If we can't do it yet.
--Find the most urgent one to cure.
	for k, v in ipairs (herbPriority) do
		if (user.affs[v.name] and
		try.salve ~= v.name and --Checking to make sure something else isn't curing it.
		try.smoke ~= v.name and 
		try.purge ~= v.name and
		try.focus ~= v.name and
		try.tree ~= v.name) then
			if (user.affs["stupidity"]) then --Stupidity check.
				try.herb = v.name --Allows us to check others aren't curing the same affliction.
				balance.herb = false --Protects us from spamming ourself.
				eat(v.cure)
				eat(v.cure)
				eat(v.cure)
				timerOn("herb", v.name) --a simple reset timer function using stopwatches
				return
			else
				try.herb = v.name
				balance.herb = false
				eat(v.cure)
				timerOn("herb", v.name)
				cecho("<white>(<blue>Herb Curing " .. v.name .. "<white>)")
				return  -- done 
			end --Ending stupidity check.
		end --No longer curing.
	end -- Ending the 'For' loop
end --Ending function.

--Replication the above for every cure type(herb,salve,smoke,purge,focus,tree,writhe)

function doAll() --Called on every prompt
    writheCuring()
    herbCuring()
    salveCuring()
    smokeCuring()
    purgeCuring()
    focusCuring()
    treeCuring()
end
So which method do you guys think is faster? Also, still would like to know how I could check this own my own without needing to know a bunch of coding logic etc...

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Speed of scripts

Post by Heiko »

You can use the system speed indicator S: value on the far right of the command line. This shows the time in milliseconds that the last line needed to process (triggers + all scripts).

I might add some profiling tool though.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: Speed of scripts

Post by tsuujin »

I'd say the difference is probably negligible on a moderately sided system. I'd worry more about which is easier for you to debug later.

Delrayne
Posts: 159
Joined: Tue Jun 07, 2011 7:07 pm
Contact:

Re: Speed of scripts

Post by Delrayne »

^ That's a good point. Being a fairly new, self taught, wanna be Lua programmer I tend to forget about that aspect. As well the difference probably is negligible.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: Speed of scripts

Post by tsuujin »

my curing system doesn't hardcode anything into the curing script. It's just an engine that parses through a user-defined set of arrays that handles afflictions, balances, cures, special usage instructions, etc. I like it because the actual curing code is very small and easy to debug, and the arrays themselves are simple. Added bonus, it works without modification on any mud that I've come across.

Post Reply