Sometimes it's nice to be wrong (Lua performance)

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
Durante
Posts: 65
Joined: Sun Oct 02, 2005 6:07 am
Location: Austria

Sometimes it's nice to be wrong (Lua performance)

Post by Durante »

I was pretty disappointed just now that -- after finally implementing one of the more complex things about my game correctly -- Lua seemed to be far slower than I expected, even though I included many early exits, etc.

Well, I tried various possibilities of speeding things up, but none helped much. I already contemplated the horrifying possibility of implementing my game in C when I finally saw the impact of line [1].

Code: Select all

	Circle:eachLine(function(p1, p2)
		-- do nothing for current line
		if not (p1 == self.cur_point or p2 == self.cur_point) then 
			screen:print(20, 30, "oki", white) -- [1]
			-- check for hit on horizontal line
			if hor and not(self.dir[2]==0) and self.y == p1[2] and
					is_bound(self.x, p1[1], p2[1]) then -- we hit it
				self.dir = {math.sgn(p2[1]-p1[1])*2, 0} -- follow
				if not self.cur_point then -- are we free roaming?
					self.cur_point = Fibers:resolve(self.x, self.y, p2)
				else self.cur_point = p1 end
				return
			-- check for hit on vertical line
			elseif not(self.dir[1]==0) and self.x == p1[1] and
					is_bound(self.y, p1[2], p2[2]) then -- we hit it
				self.dir = {0, math.sgn(p2[2]-p1[2])*2} -- follow
				if not self.cur_point then -- are we free roaming?
					self.cur_point = Fibers:resolve(self.x, self.y, p2)
				else self.cur_point = p1 end
				return
			end
		end
		hor = not hor 
	end)
Hmpf. While prematurely optimizing everything, I overlooked a debug print to the screen that was executing every single iteration. I commented it out, and now my I'm back at a very easy 60 FPS.

What to learn from that? Lua is no way that slow, and make sure that you're doing only what you think you're doing.

While I'm giving advice:
If you use the fast method of developing and are not restarting LuaPlayer between tests do not use require. Use dofile. This was already pointed out by Sprak here but is important enough to mention again (and again, and again). I knew about it and even posted in that thread but still fell into the trap. It can cause very subtle bugs where some files are a new version while others still do the old thing.
Post Reply