Which library should I use for most of UI elements? Surface/Draw?
I mean, I prefer draw, but it all depends on performance for me.
Draw just uses surface anyway.
Performance wise, it really does not matter.
Alright, thanks mate!
From the wiki page:
The draw library's purpose is to simplify the usage of the surface library.
Sometimes you need to use surface, but most of the time draw does the job
You can find the source code for the draw library in 'lua/includes/modules/draw.lua'
Awesome! Thanks for the information guys! <3
Use surface if you want better performance.
Use draw if you want less code.
As long as you never use draw.RoundedBox to draw a rectangle with no rounded corners it's ok
This is the source code of draw.RoundedBox:
--[[---------------------------------------------------------
Name: RoundedBox( bordersize, x, y, w, h, color )
Desc: Draws a rounded box - ideally bordersize will be 8 or 16
Usage: color is a table with r/g/b/a elements
-----------------------------------------------------------]]
function RoundedBoxEx( bordersize, x, y, w, h, color, tl, tr, bl, br )
surface.SetDrawColor( color.r, color.g, color.b, color.a )
-- Do not waste performance if they don't want rounded corners
if ( bordersize <= 0 ) then
surface.DrawRect( x, y, w, h )
return
end
--...
end
So it really doesn't matter if you use a 0 border size. It's really not any slower.
But this is the code for RoundedBox:
function RoundedBox( bordersize, x, y, w, h, color )
return RoundedBoxEx( bordersize, x, y, w, h, color, true, true, true, true )
end
So firstly, it calls another function. Secondly, in that function it does this:
if ( bordersize <= 0 ) then surface.DrawRect( x, y, w, h ) return end
Although these two additional steps are microscopic in performance impact compared to just calling surface.DrawRect, you cannot deny that it is faster than calling the function directly.
One would think. It's actually the opposite.
local SysTime = SysTime
local draw = draw
local surface = surface
local c = color_white
local count = 100000
--------------------------------------------------------------------------------------------------
//surface.DrawRect:
local StartTime = SysTime()
for i = 1, count do
-- Repeat an action 100,000 times to check how long it takes on average
-- Example action:
surface.SetDrawColor(c)
surface.DrawRect(0,0,100,100)
end
local EndTime = SysTime()
local TotalTime = EndTime - StartTime
local AverageTime = TotalTime / count
print( "surface.DrawRect Total: " .. TotalTime .. " seconds. Average: " .. AverageTime .. " seconds." )
--------------------------------------------------------------------------------------------------
//draw.RoundedBox:
local StartTime = SysTime()
for i = 1, count do
-- Repeat an action 100,000 times to check how long it takes on average
-- Example action:
draw.RoundedBox(0,0,0,100,100,c)
end
local EndTime = SysTime()
local TotalTime = EndTime - StartTime
local AverageTime = TotalTime / count
print( "draw.RoundedBox Total: " .. TotalTime .. " seconds. Average: " .. AverageTime .. " seconds." )
Output from 100,000 runs:
surface.DrawRect Total: 0.40842711853043 seconds. Average: 4.0842711853043e-06 seconds.
draw.RoundedBox Total: 0.34354422140244 seconds. Average: 3.4354422140244e-06 seconds.
Source:
http://wiki.garrysmod.com/page/Global/SysTime
no its a lie
Just tested it. No change from swapping it.
The inconsistent results are from JIT localising the surface function calls inside of the draw function. Localising all functions to prevent any hashtable accesses and disabling JIT should result in a correct benchmark. By technical standards, doing the two surface calls straight out is less instructions than the draw call since it's one less function call, but the difference is so minimal that it would never make a noticeable difference.
Does that mean there's no point in localizing tables in GMod? Does JIT localize everything all the time?
No, just under very certain conditions to where the tracer can do so itself. You can find more info here.
At any rate, it's a really silly optimization. draw.RoundedBox(0) just results in fewer lines and cleaner code since you set the color in the 6th argument instead of a separate statement.
My main problem with the draw library is it hiding implementation details of surface.* functions since setting the colour/text-size/position/etc. can affect later draw/surface function behaviour. I suppose as long as the code uses one or the other consistently it would be fine in that case, but the draw library is very limited in that regard.
Sorry, you need to Log In to post a reply to this thread.