CPU: Difference between revisions

From Pico-8 Wiki
Jump to navigation Jump to search
imported>Sam Hocevar
No edit summary
imported>Sam Hocevar
Line 26: Line 26:
|-
|-
| [[rect|<code>rect()</code>]] || <code>max(1,2*(A+B-1))</code>
| [[rect|<code>rect()</code>]] || <code>max(1,2*(A+B-1))</code>
||<code>w,h = abs(x2-x1),abs(y2-y1)</code>
||<code>w,h = abs(x2-x1),abs(y2-y1)


<code>A = ceil(max(w,h)/4)</code>
A = ceil(max(w,h)/4)


<code>B = ceil(max(1,min(w,h))/2)</code>
B = ceil(max(1,min(w,h))/2)</code>
|-
|-
| [[rectfill|<code>rectfill()</code>]] || <code>max(1,flr(n/16))</code> || <code>n</code> is the number of pixels drawn (width × height)
| [[rectfill|<code>rectfill()</code>]] || <code>max(1,flr(n/16))</code> || <code>n</code> is the number of pixels drawn (width × height)

Revision as of 23:19, 19 December 2018

Pico-8 keeps track of CPU usage using two values: a Lua instruction usage, and a system usage.

The function stat(1) returns the total (Lua + system) usage for the current frame, and stat(2) returns the system usage.

Only a few functions have a system usage, measured in ticks. There are 4194304 ticks in a second (2^22), so about 69905 ticks per frame at 60 FPS.

Functions with a system cost

Function Ticks Notes
cls() 1024 same cost as rectfill of same size
print() 2+n*8 n is the number of characters in the string, even those not rendered

spaces, newlines, and double-width glyphs each count as one character

spr() n n is the number of pixels drawn, including transparent pixels (width × height of the sprite rectangle)

cost is 0 if first argument is outside the [0, 255] range

sspr() n n is the number of pixels drawn, including transparent pixels (width × height of the destination rectangle)
rect() max(1,2*(A+B-1)) w,h = abs(x2-x1),abs(y2-y1)

A = ceil(max(w,h)/4)

B = ceil(max(1,min(w,h))/2)

rectfill() max(1,flr(n/16)) n is the number of pixels drawn (width × height)
circ()
circfill()
line() ceil(n/2) n is the number of pixels drawn; there is an additional cost of 1 if at least one pixel had to be clipped
map() / mapdraw() max(1,n*64) n is the number of sprites rendered; only cells that are not zero in the map are considered
music() 16 no cost if no argument
sfx() 16 no cost if no argument
memcpy() n+1 n is the number of bytes copied
memset() max(1,ceil(n/2)) n is the number of bytes set
reload() max(1,n*8) n is the number of bytes reloaded
btn() 4 no cost if no argument
btnp() 4 no cost if no argument
rnd() 4
sqrt() 24 only 16 if argument is zero
stat() 16