Jump to content

How to reduce resource CPU usage ? :'(


darhal

Recommended Posts

  • Moderators

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
  • Moderators
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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...