Log In  

Cart #40026 | 2017-04-29 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
4


Here is a function I made that is small and simple but may be useful to everyone. It launches a bunch of functions you've set, checking how many times each can be called in a frame and print sorted results so that the quickest function shows first. So, tailor made for optimisation or just know which is the fastest of many functions achieving the same goal. For example I needed to answer this agonizing question: when drawing a single big "+" sign, is it better to use 2 rectfills but draw twice the center zone, or to draw 3 rectfills (middle bar, plus left and right bars) so that no pixel is drawn twice? Now I'm ready and prepared to meet the Sphinx.

The running example here shows what is the fastest way to clear a screen between a cls or a rectfill (well, I made the wrong choice for months!) Just for fun I added clearing the screen with a bunch of lines and even psets. Surprisingly, lines are not that a bad choice against cls. Maybe there are more efficient ways I don't know, with peek or so. With this function they're easy to test.

The use and process are simple, the code above should be clear enough. If not, just declare an array containing your functions to test, each as an object with 2 properties :

  • name: used for showing results
  • f: your function

Then in _update (or _update60 if you want to get results every 1/60 second) call stat1comparator() with your functions array as argument. stat1comparator will then launch each function as many times it is possible before going beyond one frame and show results once it's done. For precision results also show how many iteration were called before going outside of the frame, and what were the precise time needed (with the limit of Pico float precision). The ratio iterations/time is used to sort the list. Just be aware that to get consistent results stat1comparator needs entire frames, so don't call it after any other processing.

And for the "+" enigma answer, I let you find by yourself :).


Twitter | Facebook | Pinterest | RedBubble

P#40027 2017-04-29 11:55 ( Edited 2017-04-29 20:34)

I got strange results with rectfills. Doing one rectfill on the whole screen is almost 10x slower than doing 128 one pixel wide rectfills. But doing the same with 128 unfilled rects or 128 lines is the worst, which makes more sense. Just copy-paste the code below. I don't know if the pb comes from my code or pico's stat(1).

function _init()
 rectfills={
  {name="1*128 rectfill", f=
   function()
    rectfill(0,0,127,127,1)
   end
  },
  {name="4*32 rectfill", f=
   function()
    for n=0,3 do
     rectfill(n*32,0,n*32+31,127,1)
    end
   end
  },
  {name="8*16 rectfill", f=
   function()
    for n=0,7 do
     rectfill(n*16,0,n*16+15,127,1)
    end
   end
  },
  {name="16*8 rectfill", f=
   function()
    for n=0,15 do
     rectfill(n*8,0,n*8+7,127,1)
    end
   end
  },
  {name="128*1 rectfill", f=
   function()
    for n=0,127 do
     rectfill(n,0,n,127,1)
    end
   end
  },
  {name="128 lines", f=
   function()
    for n=0,127 do
     line(n,0,n,127,1)
    end
   end
  },
  {name="128*1 rect", f=
   function()
    for n=0,127 do
     rect(n,0,n,127,1)
    end
   end
  },
 }
end

function _update()
 stat1comparator(rectfills)
end

function stat1comparator(funcs)
 if(funcn==nil)funcn=1
 if funcn<=#funcs then
  local func=funcs[funcn]
  func.qty,func.stat1=0,0
  while func.stat1<1 do
   func.f()
   func.qty+=1
   func.stat1=stat(1)
  end
  func.ratio=func.qty/func.stat1
  funcn+=1
 else
  --sort from fastest to slowest
  for funcn=1,#funcs do
   local i=funcn
   while i>1 and funcs[i-1].ratio<funcs[i].ratio do
    funcs[i],funcs[i-1]=funcs[i-1],funcs[i]
    i-=1
   end
  end
  cls() color(7)
  for func in all(funcs) do
   print(func.ratio.." "..func.name.." ("..func.qty.."/"..func.stat1..")")
   color(13)
  end
  print("--done--")
 end
end
P#40035 2017-04-29 16:34 ( Edited 2017-04-29 20:36)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 15:44:08 | 0.011s | Q:16