1 local sample = require('sampleprof')
15 collectgarbage('stop')
16 local profile = sample(1,100,function(n)
26 -- Print a heatmap of the results:
27 if arg[1] == '-p' then
28 local f = io.open('test.lua')
32 local line = f:read("l")
33 if not line then break end
35 maxline = math.max(#line, maxline)
38 local max, total = 0, 0
39 for k,v in pairs(profile) do
40 local filename = k:match("([^:]*):")
41 if filename == 'test.lua' then
42 max = math.max(max, v)
47 for i, line in ipairs(lines) do
48 local count = (profile[("test.lua:%d"):format(i)] or 0)
49 local percentmax = count/(max+1)
50 local k = math.floor(6*percentmax^.25)
51 local r,g,b = k+math.min(k,5-k),5-k+math.min(k,5-k),0
52 if count == 0 then r,g,b = 2,2,1 end
53 local color = 16+36*r+6*g+b
54 print(("\x1b[2m%3d\x1b[0m \x1b[38;5;%dm%s%s\x1b[0m"):format(i, color, line, (" "):rep(maxline-#line)))