-
Notifications
You must be signed in to change notification settings - Fork 14
/
debug.lua
78 lines (70 loc) · 1.73 KB
/
debug.lua
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
--
-- various functions useful for debugging and profiling
--
-- Contributors: sulai
table={}
---
-- Use this for some basic memory profiling.
-- this function counts the number
-- of entities (deep), not bytes
-- @param t table to get the size of
-- @return size in number of entities (recursive)
function table.size(t)
local size=0
for _,v in pairs(t) do
size=size+1
if type(v)=="table" then
size=size+table.size(v)
end
end
return size
end
-- test
--print(table.size({true,1,{false,"---"}}))
--- converts anything to string
function tostring(any)
if type(any)=="function" then return "function" end
if any==nil then return "nil" end
if type(any)=="string" then return any end
if type(any)=="boolean" then return any and "true" or "false" end
if type(any)=="number" then return ""..any end
if type(any)=="table" then -- recursion
local str = "{ "
for k,v in pairs(any) do
str=str..tostring(k).."->"..tostring(v).." "
end
return str.."}"
end
return "unkown" -- should never show
end
-- tests
--print(""..tostring({true,x=7,{"nest"}}))
--print(function() end)
--- do some basic cpu profiling
-- Example usage:
--
-- check_cpu()
-- ... do some intensive stuff ...
-- check_cpu("this took") -- log on system console
function check_cpu(msg)
local percent=0
if msg~=nil then
percent = ((stat(1)-check_cpu_start)*100)
printh(msg.." "..flr(percent).."% of a frame")
end
check_cpu_start=stat(1)
return percent
end
-- tests
--check_cpu()
--for i = 1, 10000 do printh("consume some time") end
--check_cpu("for loop")
--for i = 1, 20000 do printh("consume more time") end
--check_cpu("second for loop")
function print_cpu_mem()
color(7)
camera(0,0)
cursor(0,0)
print("cpu "..flr(stat(1)*100).."%")
print("mem "..stat(0))
end