Skip to content

Instantly share code, notes, and snippets.

@DoctorGester
Created November 23, 2018 23:25
Show Gist options
  • Save DoctorGester/43cd401d8c40e56e50d8d3286f3f376f to your computer and use it in GitHub Desktop.
Save DoctorGester/43cd401d8c40e56e50d8d3286f3f376f to your computer and use it in GitHub Desktop.
local histograms = {}
local function make_debug_histogram(name, length, pattern)
return {
name = name,
times = {},
writes = 0,
length = length,
pattern = pattern
}
end
local function histogram_insert_value(histogram, value)
histogram.times[(histogram.writes % histogram.length) + 1] = value
histogram.writes = histogram.writes + 1
end
local function histogram_values(histogram)
local lower_bound = histogram.writes % histogram.length
local upper_bound = lower_bound + histogram.length
local index = lower_bound
return function()
if (index == upper_bound) then
return nil
end
local value = histogram.times[(index % histogram.length) + 1] or 0
index = index + 1
return (index - lower_bound), value
end
end
function debug_plot_over_time(name, value, pattern)
local histogram = histograms[name]
if not histogram then
histogram = make_debug_histogram(name, 300, pattern)
histograms[name] = histogram
end
histogram_insert_value(histogram, value)
end
local function draw_histogram(histogram, x, y, height)
local max_value = -0xffffff
local min_value = 0xffffff
local sum = 0
for _, value in ipairs(histogram.times) do
max_value = math.max(value, max_value)
min_value = math.min(value, min_value)
sum = sum + value
end
local avg = sum / histogram.length
love.graphics.setColor(1, 0, 0)
for index, value in histogram_values(histogram) do
local line_height = (value / max_value) * height
love.graphics.line(x + index, y, x + index, y - line_height)
end
local text = string.format("%s, avg: " .. histogram.pattern, histogram.name, avg)
love.graphics.print(text, x, y + 6)
love.graphics.print(string.format(histogram.pattern, max_value), x - 60, y - height)
love.graphics.print(string.format(histogram.pattern, min_value), x - 60, y - (min_value / max_value) * height)
end
function draw_histograms()
local screen_width = love.graphics.getWidth()
local y = 140
for _, histogram in pairs(histograms) do
draw_histogram(histogram, screen_width - histogram.length - 10, y, 100)
y = y + 140
end
end
function do_and_measure_ms(func, ...)
local t = love.timer.getTime()
func(...)
return (love.timer.getTime() - t) * 1000
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment