Intended for Advanced Computers, a small benchmark is what I wrote.
Not accurate it is, but the job it does.
http://pastebin.com/2PkFLCfGSpoiler
local x,y = term.getSize()
local buffer = dofile("api/buffer")
local test = buffer.new()
local trials = ... and ... or 10
local tstart,tend,tstart_buffer,tend_buffer = 0
term.clear()
--start benchmark of regular term api
tstart = os.clock()
for trial=trials,0,-1 do
term.setCursorPos(1,1)
for j=0,y do
for i=0,x do
term.setCursorPos(i,j)
term.setBackgroundColor((2^math.random(0,15)))
term.setTextColor((2^math.random(0,15)))
term.write(tostring(math.random(0,10)))
end
end
end
tend = os.clock()
sleep(.2)
term.clear()
--start benchmark of buffer api
tstart_buffer = os.clock()
for trial=trials,0,-1 do
test.pos_x = 1
test.pos_y = 1
for j=0,y do
for i=0,x do
test.pos_x = i
test.pos_y = j
test.back = (2^math.random(0,15))
test.text = (2^math.random(0,15))
test:write(tostring(math.random(0,10)))
end
end
end
test:draw()
tend_buffer = os.clock()
--done with generation
sleep(.2)
term.setCursorPos(1,1)
term.setBackgroundColor(0x8000)
term.setTextColor(0x1)
term.clear()
term.write("Trials: "..(trials).." Unbuff: "..(tend-tstart).." buff: "..(tend_buffer-tstart_buffer).." diff: "..(math.abs((tend-tstart)-(tend_buffer-tstart_buffer))))
term.setCursorPos(1,2)
Run in singleplayer Minecraft with the latest ComputerCraft version, I was returned with:
>buffer_test
Trials: 10 Unbuff: 0.15 buff: 0.25 diff: 0.1
>buffer_test 20
Trials: 20 Unbuff: 0.3 buff: 0.6 diff: 0.3
>buffer_test 100
Trials: 100 Unbuff: 1.4 buff: 2.6 diff: 1.2
This is severely inaccurate due to how the os.clock is updated (per tick)
Testing this out in LOVE CC Emulator caused a stack overflow
Can someone double-check my code to make sure I didn't make a mistake or an inconsistency between the term and test loops?
Edit:I realize how the above code does not actually provide a legitimate benchmark due to how the terminal draws and how the buffer api draws. It seems that the term api draws whenever there's an available time slice, probably through routines, while the buffer api draws after the whole frame has been generated.
I'm going to try this out with the os.loadAPI method and see how its performance compares.
Edit 2:Results for using os.loadAPI method (with modified code): - NOT BETTER WHAT SO EVER
Spoiler
>buffer_test
Trials: 10 Unbuff: 0.15 buff: 0.9 diff: 0.75
>buffer_test 20
Trials: 20 Unbuff: 0.3 buff: 2.5 diff: 2.2
>buffer_test 30
Trials: 30 Unbuff: 0.45 buff: 4.65 diff: 4.2
--Anything over some number in (30,40) will cause the lua interpreter to kick in with "Too long without yielding"
modified api code:
http://pastebin.com/LYiuHgxQ
modified test code:
http://pastebin.com/3d733c3d