Jump to content

High CPU Usage


undefined

Recommended Posts

I'm use this function to draw text outline. But it's use %13 CPU. I think it's so big for only draw outline.

How can i decrease it?

function tocolorToRGBA(color) 
    return bitExtract(color, 0, 8 ), bitExtract(color, 8, 8 ), bitExtract(color, 16, 8 ), bitExtract(color, 24, 8 ) 
end 
  
local _dxDrawText = dxDrawText 
function dxDrawText(...) 
    local args = {...} 
    args[4] = args[2] + args[4] 
    args[5] = args[3] + args[5] 
    return _dxDrawText(unpack(args)) 
end 
  
function dxDrawTextOutline(...) 
    local args = {...} 
    local _, startX, startY = unpack(args) 
     
    local color = args[6] 
    local _,_,_,a = tocolorToRGBA(color) 
    args[6] = tocolor(0,0,0,a) 
     
    args[2], args[3] = startX-1, startY-1 
    dxDrawText(unpack(args)) 
    args[2], args[3] = startX-1, startY+1 
    dxDrawText(unpack(args)) 
    args[2], args[3] = startX+1, startY+1 
    dxDrawText(unpack(args)) 
    args[2], args[3] = startX+1, startY-1 
    dxDrawText(unpack(args)) 
     
    args[2], args[3], args[6] = startX, startY, color 
    dxDrawText(unpack(args)) 
end 
  
addEventHandler("onClientRender", root, functon() 
    dxDrawOutlineText(text, x, y, ...) 
end) 

Edited by Guest
Link to comment

Absolutely not

I'll tell you the difference:

Your code:

You use the function in every frame.

When you have 60 FPS , your function has to get called 60 times per second.

Thats 60x tocolorToRGBA, 300x dxDrawText ...

RenderTarget:

You create a RenderTarget, set the render target to this rendertarget and use your dxDrawOutlineText only 1 time!

After that you clear the render target and use your rendertarget-texture:

You just draw the rendertarget every frame.

Thats only 60x dxDrawImage per Second

Much better performance.

example:

http://www2.pic-upload.de/img/29634650/mta-screen_2016-01-16_05-24-27.png

This window is just one renderTarget, 1 dxDrawText and 1 dxDrawRectangle.

That means onClientRender uses only 1 dxDrawImage, 1 dxDrawText and 1 dxDrawRectangle every second.

Without the renderTarget onClientRender-function would have to call atleast 8 dxDrawImage, 12 dxDrawText, 45 dxDrawImage and 4 dxDrawLine every second!!

Which one is better?

Link to comment

Well, dxDrawText itself it a pretty CPU hungry function and you got at least 5 of those (usually gives around 10% CPU usage), you could build your text as a single multi line string first and then send it to the draw function once to reduce your CPU usage to 2% maybe 3% if your lucky. Table unpack is quiet heavy too, maybe you could skip that and pass the data directly as it is using index numbers instead.

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...