darhal Posted June 13, 2015 Share Posted June 13, 2015 Hello all, Can anyone tell my how to reduce resource cpu usage ? thx all Link to comment
TAPL Posted June 13, 2015 Share Posted June 13, 2015 https://wiki.multitheftauto.com/wiki/Debugging#Debugging_Performance_Issues Link to comment
Moderators IIYAMA Posted June 15, 2015 Moderators Share Posted June 15, 2015 By reducing database/xml accessing. It uses the most of the memory and is most of the time the thing that will crash your server. Also keep an eye on infinity loops or tables that don't get cleaned(memory leak). Link to comment
darhal Posted June 16, 2015 Author Share Posted June 16, 2015 Th ilyama but what about a loop inside function attached to onClientRender event ? Link to comment
Dealman Posted June 16, 2015 Share Posted June 16, 2015 Th ilyama but what about a loop inside function attached to onClientRender event ? Depends on what you're trying to do, and how you've currently made it. Can't really help you optimize it without knowing what it is Link to comment
Moderators IIYAMA Posted June 16, 2015 Moderators Share Posted June 16, 2015 Th ilyama but what about a loop inside function attached to onClientRender event ? It will use more memory than static code, that is a fact. But what more important is, is what happens inside of that loop and how many times it got executed. Do you want to know if your code is lagging? You will know if your timeSlice starts increasing. (see default timeSlice) When your code speed is slower than the default timeSlice, your fps will drop and then you know that it is lagging. But this is per pc differed of course. With the code below my execution time is 5/6 ms. Default timeSlice: 60 fps will have circa an execution time of 17 ms. How do I get this value? It is frames per second so: 1000 ms / 60 fps = 16,666666666666666666666666666667 addEventHandler("onClientPreRender",root, function (timeSlice) local startTime = getTickCount() for i=1,10000 do -- add your code here -- local x,y,z = getElementPosition(localPlayer) ------------------------ end for i=1,10000 do -- add your code here -- local x,y,z = getElementPosition(localPlayer) ------------------------ end for i=1,10000 do -- add your code here -- local x,y,z = getElementPosition(localPlayer) ------------------------ end dxDrawText("Code speed: " .. getTickCount()-startTime .. " ms, time between frames: " .. timeSlice .. " ms.",300,300) end) Link to comment
Walid Posted June 16, 2015 Share Posted June 16, 2015 About loops (Performance) Code 1 : for i=1,1000000 do for j,v in pairs(a) do x=v end end Result : pairs: 3.078 (217%) Code 2 : for i=1,1000000 do for j,v in ipairs(a) do x=v end end Result: ipairs: 3.344 (236%) Code 3 : for i=1,1000000 do for i=1,100 do x=a[i] end end Result: for i=1,x do: 1.422 (100%) Code 4: for i=1,1000000 do for i=1,#a do x=a[i] end end Result: for i=1,#atable do 1.422 (100%) Code 5: for i=1,1000000 do local length = #a for i=1,length do x=a[i] end end Result: for i=1,atable_length do: 1.562 (110%) Conclusion: Don't use pairs() or ipairs() in critical code! Try to save the table-size somewhere and use for i=1,x do! Link to comment
darhal Posted June 16, 2015 Author Share Posted June 16, 2015 Thanks All of you help me a lot specially IIYAMA and walid , but if I had a code like bleow how to calculate timeSlice and so on this code is lagging ingame everytime I switch the mouse : function switchItemsUsingMouseWheel(btn, bool) normalSizeX = correctResolutinX(128/5) normalSizeY = correctResolutinY(114/5) startTime = getTickCount() if isCursorShowing ( ) then return end if btn == "mouse_wheel_up" then lastWheelI = lastWheelI + 1 for k, val in pairs(currentFastUseItems) do if lastWheelI == val.boxidfu and lastWheelI <= 6 then if lastKCursorWheel then local sx, sy = unpack(split(currentFastUseItems[lastKCursorWheel].size, ",")) local x, y = unpack(split(currentFastUseItems[lastKCursorWheel].pos1fu, ",")) currentFastUseItems[lastKCursorWheel].size=tostring(tonumber(sx/1.5)..", "..tonumber(sy/1.5)) currentFastUseItems[lastKCursorWheel].pos1fu=tostring(tonumber(x+8.3)..", "..tonumber(y+8.3)) end if putInUseTimer and isTimer(putInUseTimer) then killTimer(putInUseTimer) end local sx, sy = unpack(split(val.size, ",")) local x, y = unpack(split(val.pos1fu, ",")) currentFastUseItems[k].size=tostring(tonumber(sx*1.5)..", "..tonumber(sy*1.5)) currentFastUseItems[k].pos1fu=tostring(tonumber(x-8.3)..", "..tonumber(y-8.3)) lastKCursorWheel = k numberOfReapeats = numberOfReapeats + 1 putInUseTimer = setTimer(function (id, k, wheelI) for i, v in pairs(currentUseItems) do if v.item == id then table.remove(currentUseItems, i) local sx, sy = unpack(split(currentFastUseItems[lastKCursorWheel].size, ",")) local x, y = unpack(split(currentFastUseItems[lastKCursorWheel].pos1fu, ",")) currentFastUseItems[lastKCursorWheel].size=tostring(tonumber(sx/1.5)..", "..tonumber(sy/1.5)) currentFastUseItems[lastKCursorWheel].pos1fu=tostring(tonumber(x+8.3)..", "..tonumber(y+8.3)) lastKCursorWheel = nil triggerServerEvent( "onClientPlayerUseItem", localPlayer, id, 1) triggerEvent( "onClientPlayerUseItem", localPlayer, id, 1 ) return end end lastWheelI = wheelI local sx, sy = unpack(split(currentFastUseItems[lastKCursorWheel].size, ",")) local x, y = unpack(split(currentFastUseItems[lastKCursorWheel].pos1fu, ",")) table.insert(currentUseItems, {item=id, boxId=wheelI}) triggerServerEvent( "onClientPlayerUseItem", localPlayer, id, 1) triggerEvent( "onClientPlayerUseItem", localPlayer, id, 1 ) currentFastUseItems[k].size=tostring(tonumber(sx/1.5)..", "..tonumber(sy/1.5)) currentFastUseItems[k].pos1fu=tostring(tonumber(x+8.3)..", "..tonumber(y+8.3)) lastKCursorWheel = nil end, switchTime, 1, val.idfu, lastKCursorWheel, lastWheelI) break end if (lastWheelI >= 7) or (numberOfReapeats > #currentFastUseItems) then lastWheelI = 0 numberOfReapeats = 0 --[[for i, v in pairs(currentUseItems) do table.remove(currentUseItems, i) local sx, sy = unpack(split(currentFastUseItems[lastKCursorWheel].size, ",")) local x, y = unpack(split(currentFastUseItems[lastKCursorWheel].pos1fu, ",")) currentFastUseItems[lastKCursorWheel].size=tostring(tonumber(sx/1.5)..", "..tonumber(sy/1.5)) currentFastUseItems[lastKCursorWheel].pos1fu=tostring(tonumber(x+8.3)..", "..tonumber(y+8.3)) lastKCursorWheel = nil triggerServerEvent( "onClientPlayerUseItem", localPlayer, v.item, 1) triggerEvent( "onClientPlayerUseItem", localPlayer, v.item, 1 ) end--]] if putInUseTimer and isTimer(putInUseTimer) then killTimer(putInUseTimer) end if lastKCursorWheel then local sx, sy = unpack(split(currentFastUseItems[lastKCursorWheel].size, ",")) local x, y = unpack(split(currentFastUseItems[lastKCursorWheel].pos1fu, ",")) currentFastUseItems[lastKCursorWheel].size=tostring(tonumber(sx/1.5)..", "..tonumber(sy/1.5)) currentFastUseItems[lastKCursorWheel].pos1fu=tostring(tonumber(x+8.3)..", "..tonumber(y+8.3)) lastKCursorWheel = nil end end end elseif btn == "mouse_wheel_down" then lastWheelI = lastWheelI - 1 for k, val in pairs(currentFastUseItems) do if lastWheelI == val.boxidfu and lastWheelI >= 1 then if lastKCursorWheel then local sx, sy = unpack(split(currentFastUseItems[lastKCursorWheel].size, ",")) local x, y = unpack(split(currentFastUseItems[lastKCursorWheel].pos1fu, ",")) currentFastUseItems[lastKCursorWheel].size=tostring(tonumber(sx/1.5)..", "..tonumber(sy/1.5)) currentFastUseItems[lastKCursorWheel].pos1fu=tostring(tonumber(x+8.3)..", "..tonumber(y+8.3)) end if putInUseTimer and isTimer(putInUseTimer) then killTimer(putInUseTimer) end local sx, sy = unpack(split(val.size, ",")) local x, y = unpack(split(val.pos1fu, ",")) currentFastUseItems[k].size=tostring(tonumber(sx*1.5)..", "..tonumber(sy*1.5)) currentFastUseItems[k].pos1fu=tostring(tonumber(x-8.3)..", "..tonumber(y-8.3)) lastKCursorWheel = k numberOfReapeats = numberOfReapeats + 1 putInUseTimer = setTimer(function (id, k, wheelI) for i, v in pairs(currentUseItems) do if v.item == id then table.remove(currentUseItems, i) local sx, sy = unpack(split(currentFastUseItems[lastKCursorWheel].size, ",")) local x, y = unpack(split(currentFastUseItems[lastKCursorWheel].pos1fu, ",")) currentFastUseItems[lastKCursorWheel].size=tostring(tonumber(sx/1.5)..", "..tonumber(sy/1.5)) currentFastUseItems[lastKCursorWheel].pos1fu=tostring(tonumber(x+8.3)..", "..tonumber(y+8.3)) lastKCursorWheel = nil triggerServerEvent( "onClientPlayerUseItem", localPlayer, v.id, 1) triggerEvent( "onClientPlayerUseItem", localPlayer, v.id, 1 ) return end end lastWheelI = wheelI local sx, sy = unpack(split(currentFastUseItems[lastKCursorWheel].size, ",")) local x, y = unpack(split(currentFastUseItems[lastKCursorWheel].pos1fu, ",")) table.insert(currentUseItems, {item=id, boxId=wheelI}) triggerServerEvent( "onClientPlayerUseItem", localPlayer, id, 1) triggerEvent( "onClientPlayerUseItem", localPlayer, id, 1 ) currentFastUseItems[k].size=tostring(tonumber(sx/1.5)..", "..tonumber(sy/1.5)) currentFastUseItems[k].pos1fu=tostring(tonumber(x+8.3)..", "..tonumber(y+8.3)) lastKCursorWheel = nil end, switchTime, 1, val.idfu, lastKCursorWheel, lastWheelI) break end if (lastWheelI <= 0) or (numberOfReapeats > #currentFastUseItems) then lastWheelI = 7 numberOfReapeats = 0 --[[for i, v in pairs(currentUseItems) do table.remove(currentUseItems, i) local sx, sy = unpack(split(currentFastUseItems[lastKCursorWheel].size, ",")) local x, y = unpack(split(currentFastUseItems[lastKCursorWheel].pos1fu, ",")) currentFastUseItems[lastKCursorWheel].size=tostring(tonumber(sx/1.5)..", "..tonumber(sy/1.5)) currentFastUseItems[lastKCursorWheel].pos1fu=tostring(tonumber(x+8.3)..", "..tonumber(y+8.3)) lastKCursorWheel = nil triggerServerEvent( "onClientPlayerUseItem", localPlayer, v.item, 1) triggerEvent( "onClientPlayerUseItem", localPlayer, v.item, 1 ) end--]] if putInUseTimer and isTimer(putInUseTimer) then killTimer(putInUseTimer) end if lastKCursorWheel then local sx, sy = unpack(split(currentFastUseItems[lastKCursorWheel].size, ",")) local x, y = unpack(split(currentFastUseItems[lastKCursorWheel].pos1fu, ",")) currentFastUseItems[lastKCursorWheel].size=tostring(tonumber(sx/1.5)..", "..tonumber(sy/1.5)) currentFastUseItems[lastKCursorWheel].pos1fu=tostring(tonumber(x+8.3)..", "..tonumber(y+8.3)) lastKCursorWheel = nil end end end end end addEventHandler("onClientKey", root, switchItemsUsingMouseWheel) Link to comment
Moderators IIYAMA Posted June 16, 2015 Moderators Share Posted June 16, 2015 The things that are lagging here: [color=#FF0000]- timers (destroy and create) < Especially that one. (and inside a loop)[/color] - creating new functions over and over. (and inside a loop) - triggerServerEvent (uses more memory than usually) - triggerEvent (uses more memory than usually) Link to comment
darhal Posted June 16, 2015 Author Share Posted June 16, 2015 actually, I remove the timer and the functions the lag has been reduced thx a lot mate Link to comment
xXMADEXx Posted June 16, 2015 Share Posted June 16, 2015 The best way to find the harsh CPU and memory resources is to use the in-game performance browser resource. https://github.com/multitheftauto/mtasa ... min%5D/ipb Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now