• Why is my code so inefficient?
    7 replies, posted
Hey i've been creating a tiny... visual aid with LÖVE and this programme makes my CPU usage go crazy! is there anything i've done wrong or could improve on? [code] http = require("socket.http") url = require("socket.url") function love.load() block = love.graphics.newImage("content/images/Block.png") blocks = 0 chatbox = "Type in your minecraft username and press Enter" inputa = "" ent = {} speed = 100 i = 0 flag = false end function love.update( dt ) for k,v in pairs(ent) do v.pos.y = v.pos.y + (speed * dt) end while i < tonumber(blocks) do i = i + 1 createEnt() end end function love.draw() if go == true then if flag == false then for k,v in pairs(ent) do love.graphics.draw(block, v.pos.x, v.pos.y) end end end love.graphics.print(chatbox, 10, 50) love.graphics.print(inputa, 10, 100) love.graphics.print(blocks, 10, 200) if i ~= nil then love.graphics.print(i, 10, 150) end end function request(name) blocks = http.request("http://kuro.bambofy.co.uk/grab.php?user="..name.."") if blocks == nil then chatbox = "Sorry we cannot find your username in our database! please contact Bambo if this error occurs frequently." go = false else chatbox = "Success!" go = true end end function love.keypressed(k) if k == "escape" then love.event.push('q') elseif k == "return" then inputa = url.escape(inputa) request(inputa) elseif k == "backspace" then inputleng = string.len(inputa) - 1 newinput = string.sub(inputa, 0, inputleng) inputa = newinput elseif k == "lshift" then v = true inputa = inputa elseif v == true then inputa = ""..inputa..""..string.upper(k).."" else inputa = ""..inputa..""..k.."" end end function love.keyreleased( k ) if k == "lshift" then if v == true then v = false end end end function createEnt() table.insert(ent, returnInfo()) end function round(num, idp) local mult = 10^(idp or 0) return math.floor(num * mult + 0.5) / mult end function returnInfo(x, y) if x == nil then x = math.random(800) end if y == nil then y = math.random(600) y = y * -1 end return {pos={x=x,y=y}, speed=speed} end [/code] any help is appreciated, this has been annoying me for a while now.
High CPU usage doesn't necessarily make it inefficient as long as it's running fast enough.
Unless you have statistics to compare it against... In my Lua game engine, displaying a couple hundred networked entities and calculating their movement and collision on both a client and a server does not take my dual core 2.13 GHz processor up more than 35% of what it was before running the applications at any given time during the applications running. Displaying 20 lines of text on a single instance of the application does not take more than 12% of the CPU usage at any given time. I think that the OP is safe to consider his program inefficient if it's greater than double or so my tested CPU usages on a similar processor. Besides for ranting about what inefficient is, I think the best way to get an answer to your problem, OP, is to post on the LÖVE forums. Your general operations that aren't specific to LÖVE are very miniscule.
Im not really sure where LOVE enters so i cant check this, but if you have an infinite loop with no particularly expensive operations in, normally a main loop, it tends to max out a cpu. Not really a massive issue though
CPU usage doesn't necessarily reflect on the efficiency of your program. Most games are run in an infinite loop, meaning it'll render a frame, then go back and render another frame as fast as it can, meaning it'll utilize as much of the CPU as possible. You should be worried about how many milliseconds it's taking you to render a frame instead. [editline]16th April 2011[/editline] enabling VSync caps your framerate at 60 (usually), which means that the processor has time to idle between frames and the CPU utilization looks lower.
My framerate is fine, 60 FPS all time, i was just getting worried about the CPU going crazy. Thanks for all the help guys!
From what I can tell, you're making loads of entities every frame and they aren't ever getting removed. Also this: [code] if go == true then if flag == false then for k,v in pairs(ent) do love.graphics.draw(block, v.pos.x, v.pos.y) end end end [/code] Can be wrote like this: [code] if go and not flag then for k, v in pairs(ent) do love.graphics.draw(block, v.pos.x, v.pos.y) end end [/code] Just makes it a little more readable.
[QUOTE=yngndrw;29273704]From what I can tell, you're making loads of entities every frame and they aren't ever getting removed. Also this: [code] if go == true then if flag == false then for k,v in pairs(ent) do love.graphics.draw(block, v.pos.x, v.pos.y) end end end [/code] Can be wrote like this: [code] if go and not flag then for k, v in pairs(ent) do love.graphics.draw(block, v.pos.x, v.pos.y) end end [/code] Just makes it a little more readable.[/QUOTE] i see, thanks alot. I was wondering about that.
Sorry, you need to Log In to post a reply to this thread.