-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththrottler.lua
More file actions
32 lines (25 loc) · 752 Bytes
/
Copy paththrottler.lua
File metadata and controls
32 lines (25 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
-- (c) 2024 Jacek Olszak
-- This code is licensed under MIT license (see LICENSE for details)
-- creates an object which slows down the code if it runs too fast
function new_throttler(max_per_frame)
local fps <const> = 60
local instructions_per_second = 8000000 -- max no of instructions in Picotron
local started, count
local function reset()
started = time()
count = 0
end
reset()
local throttler <const> = {}
function throttler:throttle()
count += 1
if count > max_per_frame and
count / (time() - started) / fps > max_per_frame
then
-- sleep for a frame
for i = 1, instructions_per_second / fps do end
reset()
end
end
return throttler
end