Hi,
I'm a part time tinkering when it comes to coding - just dabbling now and again, and I wondered if anyone had any ideas on how to speed up the following code please?
I want to draw a screen full of random CHR$ characters, but this method is taking approx 13 seconds to run!
function _init()
cls(1)
b=1
i=1
end
function _update()
i=i+5
print(chr(rnd(128)),i,b,7)
if i>=120 then
b=b+8
i=0
if b>=125 then
stop()
end
end
end
Any help would be much appreciated :-)
Thank you
_DRAW and _UPDATE (and _UPDATE60) are frame dependent. These Functions act like a Loop, but the next "turn" will be started after a Frame, which makes it way too slow.
A possible Solution is: Make your own "Inner-Loop" that runs on exactly one Frame. Here's an example using REPEAT...UNTIL:
function _init() cls(1) end function _update() screen_full_of_chars() end function screen_full_of_chars() b=1 i=1 repeat i=i+5 print(chr(rnd(128)),i,b,7) if i>=120 then b=b+8 i=0 end until b>=125 stop() end |
I must confess that I'm not really satisfied about this Solution either, because every Command that writes something to Screen should be included inside the _DRAW-Function, like the PRINT-Command... So I've rewritten it a bit^^:
function _init() cls(1) ptable = {} end function _update() fill_ptable() end function _draw() draw_ptable() stop() end function fill_ptable() for y=1,16 do ptable[y] = {} for x=1,24 do ptable[y][x] = chr(rnd(128)) end end end function draw_ptable() for y=1,16 do for x=1,24 do print(ptable[y][x],x*5,(y-1)*8+1,7) end end end |
A 2D-Table called "ptable" includes every Char and will be filled by the "fill_ptable"-Function. Inside the _DRAW-Function, it iterates through that Table ("draw_ptable") and PRINTs every Char...
Not exactly what you asked for but here is something that allows you to mostly the same.
I see though that you were trying to seperate the chars a little bit.
see if it meets your needs or not
function _init() cls(1) b=1 i=1 flat = "" changed = false end function _update() -- could do with a string buffer if btn(❎) or flat=="" then flat="" for b = 1,125,5 do for i = 1,120,5 do -- first 32 chars are control -- and can have strange effects -- feel free to remove the -32+32 flat = flat..chr(rnd(128-32)+32) end flat = flat .. "\n" end changed =true end end function _draw() if (changed) then cls(1) print (flat,0,0,7) end end |
Wow!
Thanks for the really speedy response, and the explanations. Can't believe how much faster (almost instantaneous almost!) this method is.
I'll be sure to get my head around this and use it in the future :-)
Thanks again - much appreciated!
[Please log in to post a comment]