Log In  

mildly interesting: when drawing a perfectly vertical line (line(1,1,1,h)) or perfectly horizontal line (line(1,1,w,1)), it used to be cheaper (in cpu cycles) to use rect or rectfill instead. but not anymore! I'm testing this on 0.2.5g; I'm not sure when this changed.

tl;dr: line and rectfill cost the same for orthogonal lines (rect is more expensive for vertical lines only)

simple test:


load #prof, then add these lines:

-- horizontal
prof(function()
 line(0,0,90,0,2)
end,function()
 rect(0,0,90,0,2)
end,function()
 rectfill(0,0,90,0,2)
end)
--  9+10=19 (lua+sys)
--  9+10=19 (lua+sys)
--  9+10=19 (lua+sys)

-- vertical
prof(function()
 line(0,0,0,90,2)
end,function()
 rect(0,0,0,90,2)
end,function()
 rectfill(0,0,0,90,2)
end)
--  9+10=19 (lua+sys)
--  9+22=31 (lua+sys)
--  9+10=19 (lua+sys)

full test:

function one(fn,opts)
  local fmt=function(n)
    -- leftpad a number to 2 columns
    return n<9.9 and " "..n or n
  end

  local dat=prof_one(fn,opts)
  printh(fmt(dat.lua)
    .."+"
    ..fmt(dat.sys)
    .."="
    ..fmt(dat.total)
    .." (lua+sys)")
end

function hline(x) line(0,0,x,0,2) end
function hrect(x) rect(0,0,x,0,2) end
function hrfil(x) rectfill(0,0,x,0,2) end
function vline(x) line(0,0,0,x,2) end
function vrect(x) rect(0,0,0,x,2) end
function vrfil(x) rectfill(0,0,0,x,2) end

printh"--"
for i=0,127 do
 printh(i..": ")
 one(hline,{locals={i}})
 one(hrect,{locals={i}})
 one(hrfil,{locals={i}})
 one(vline,{locals={i}})
 one(vrect,{locals={i}})
 one(vrfil,{locals={i}})
end

--[[
...
14: 
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
15: 
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
16: 
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
 9+ 4=13 (lua+sys)
 9+ 2=11 (lua+sys)
17: 
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
 9+ 2=11 (lua+sys)
 9+ 4=13 (lua+sys)
 9+ 2=11 (lua+sys)
...
]]

summary:

rect:
    when h is 1, sys cost is:
        max(1,w\16)*2 (agrees with CPU wiki page)
    when w is 1, sys cost is:
        max(1,(h-1)\8)*2 (disagrees? unsure)
line and rectfill
    a,b = max(w,h),min(w,h)
    when b is 1, sys cost is:
        max(1,a\16)*2 (agrees with CPU page for rectfill, but not line(?))

P#127584 2023-03-25 03:47


[Please log in to post a comment]