Skip to content

API Performance

Performance monitoring utilities for apps.

The OS can count frames and draw an FPS counter over any app, Lua or native, with no app code: system menu → Settings → Show FPS. Each press of Enter moves it on: Off → Top right → Top left → Bottom right → Bottom left, so you can move a counter that covers your HUD. The setting is the show_fps key in API Sysconfig and takes effect at your app’s first present after the menu closes: until then the screen the menu gives back shows the counter as your last present left it (at the old corner, with its old number). The counter is drawn only while an app runs, not in the launcher or over the system menu.

  • What it counts. Frames presented per second, counted over a window of at least 1 s: flush() and flushRegion() count one frame each. For an app that presents only with flushRows(), a call whose y0 is at or above the previous call’s y0 starts a new frame, so a top-to-bottom sweep of bands is one frame.
  • perf.endFrame() sets the number. While your app calls endFrame() (within the last 2 s), the counter shows endFrame() calls per second instead: your loop’s logical frame rate, even when you present less often (an emulator running two game frames per present shows 60, not 30). It is counted per 1 s window, not from the 30-frame average getFPS() returns, so the two can differ by a frame or two.
  • What it looks like. FPS: n in the built-in 6×8 font, colour-coded like drawFPS(): green ≥55, yellow ≥30, red below; FPS: -- in grey until the first second is counted; numbers stop at 999. It sits in an opaque black 52×12 box with its text 8 px in from the screen edges; the top corners are just below the standard header (picocalc.ui.drawHeader), where drawFPS() draws by default.
  • Where it is drawn. The OS draws it into your frame just before each present, so after flush() it is in your framebuffer and in screenshots. With flushRows() and flushRegion() it is drawn only into the rows you send, and stays in your buffer there. When the box lies outside those rows the OS puts it on the panel directly (only when its text changes) and leaves your buffer as it was, so a screenshot on the device, which reads the framebuffer, does not show it. When the counter moves or is turned off, the OS pushes your buffer’s pixels back over it; if an earlier flush sent its rows (so it went into your buffer) and you have not redrawn them since, the old box stays until you do, as it would with flush().
  • Your own counter. While Show FPS is on, drawFPS() draws nothing: the OS counter replaces it, so only the OS counter shows, at the corner the player chose, wherever your app would draw its own. With Show FPS off, drawFPS() draws normally. A number you draw yourself from getFPS() is not affected.
  • Hardware scroll. While the scroll offset (setScrollOffset) is not 0, nothing is drawn. The next partial flush pushes your buffer’s rows back over the box, so it does not slide with your content, unless the box had gone into your buffer (see above): then it is part of your image and scrolls away with it. It comes back once the offset is 0.

drawFPS() and getFPS() stay for apps that want their own counter.

Starts timing a frame. Call at the beginning of your game loop.

  • Parameters: None
  • Returns: None
while true do
picocalc.perf.beginFrame()
-- Game logic
picocalc.perf.endFrame()
end

Ends timing a frame and updates FPS calculation. Call at the end of your game loop. While you call it, the OS FPS counter (Show FPS, above) shows your endFrame() calls per second.

  • Parameters: None
  • Returns: None

Returns the frames presented per second, averaged over the last 30 frames: the time from one endFrame() to the next, including the wait setTargetFPS adds. A game paced at 30 reports 30 however little work its frames take.

  • Parameters: None
  • Returns: (number) Frames per second
local fps = picocalc.perf.getFPS()

Returns the last frame’s work in milliseconds: from the previous endFrame() (or beginFrame(), for the first frame) to this endFrame(), without the setTargetFPS wait. Compare it with the target’s frame period to see the headroom.

  • Parameters: None
  • Returns: (number) Milliseconds
local ms = picocalc.perf.getFrameTime()

Convenience function to draw the FPS counter on screen: FPS: n with n from getFPS(), in the current font on black. Color-coded: green ≥55 FPS, yellow ≥30, red <30. While the OS counter is on (Show FPS, above) it draws nothing, so only the OS counter shows. Native apps have the same call, api->perf->drawFPS(x, y).

The position is used as given, not moved onto the screen: like drawText, text past the edge (or outside the clip rect) is cut off. In the built-in 6×8 font FPS: 60 is 42 px wide and FPS: 100 48 px; leave x out to right-align it.

  • Parameters:
    • x (number, optional): X coordinate. Defaults to the top-right corner: the text ends 8px from the right edge.
    • y (number, optional): Y coordinate. Defaults to 24, just below the standard header (picocalc.ui.drawHeader), so the counter never covers its status icons.
  • Returns: None
picocalc.perf.drawFPS() -- Draw at default position

Set target frame rate for automatic frame pacing. When set, endFrame() waits until the frame’s deadline, which advances by 1/fps from the previous one (in microseconds, so 30 fps is 33 333 µs, not 33 ms). A frame that ends late restarts the schedule from that moment instead of rushing later frames. Pass 0 to disable frame limiting.

While endFrame() waits, the second core does work that would otherwise slow your frames: the MP3Player decodes ahead then. A frame that ends late has no wait, so leave some time to spare.

  • Parameters:
    • fps (number): Target frames per second (0 = unlimited)
  • Returns: None
picocalc.perf.setTargetFPS(30) -- Cap at 30 FPS
picocalc.perf.setTargetFPS(0) -- Disable frame limiting