Script: Code Execution Time Tester

Post Reply
Sanaki
Posts: 110
Joined: Wed Mar 09, 2011 1:30 am

Script: Code Execution Time Tester

Post by Sanaki »

Ever wanted to test how long a script you wrote takes to run, which method of two is faster, etc? So did I. Iocun gave me the idea, been using this for a bit now to streamline my code a bit to work better on slower machines. Anyone interested, give it a go. I tried to leave the code as unambiguous and easy to edit as anyone would like. Please, by all means, use it as you see fit.
Also, I find it fascinating that x ="5"*1 is almost twice as fast as x=tonumber("5"). Just throwing that out there.
Code: [show] | [select all] lua
testwatch = testwatch or createStopWatch()
-- edit iterations to change how many times the code loops
local iterations = 1000000
-- edit timelimit to change how long maximum, in seconds, it will be allowed to loop
local timelimit = 5

startStopWatch(testwatch)
for i = 1,iterations
do
	--stick code to test under here

	--stick code to test above here
	if i == iterations then
		local speed = stopStopWatch(testwatch)
		cecho("\nScript ran "..iterations.." iterations of the specified code in: <red>"..speed)
		resetStopWatch(testwatch)
	elseif getStopWatchTime(testwatch) >= timelimit then
		cecho("\nTime limit of <red>"..timelimit.."<grey> seconds reached after <red>"..i.."<grey> iterations, stopwatch aborted.")
		stopStopWatch(testwatch)
		resetStopWatch(testwatch)
		break
	end
end
Instructions:
Change iterations to how many times you want it to loop, change timelimit to when you want it to halt. The latter is a failsafe. Don't go ramping it up just to let it run fully. From there, just put your code between the designated comments and let it go. Depending on complexity, could need 10 iterations, could need a million.

Warnings:
1. Yes, I know this loops and tests time taken. Dear gods, don't ever put send() or sendAll() in this if you're connected to -any- server. Any code that will send to a server or somehow affect anything outside your machine shouldn't be used in this.
2. Yes, this will freeze Mudlet while it runs. That's the joy of looping inane amounts of code. It's not broken. If you reach the end of your timelimit time and it's still frozen... well, best take a look at what you sent through this, hadn't you?
3. Yes, if you put bad/broken code through this carelessly, it could freeze up quite nicely. If you lose reflexes because you failed to save, no pointing fingers at me. This is a tool, I wouldn't suggest a drunk man uses a hammer any faster.

Post Reply