Difference between revisions of "Core.profilerReport"

From GiderosMobile
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__NOTOC__
+
'''Available since:''' Gideros 2017.8<br/>
'''<translate>Available since</translate>:''' Gideros 2017.8<br/>
+
'''Class:''' [[Core]]<br/>
=== <translate>Description</translate> ===
+
 
<translate>Returns an associative array of tables describing CPU usage of lua functions. Each table in the array is indexed by an unique function id and has the following content:
+
=== Description ===
*name* - Name of the function
+
Returns an associative array of tables describing CPU usage of lua functions.
*time* - Total time spent in the function (seconds)
+
<syntaxhighlight lang="lua">
*count* - Number of times this function was called
 
*callers* - A table containing the same information has above for each function which called this one</translate>
 
<source lang="lua">
 
 
(table) = Core.profilerReport()
 
(table) = Core.profilerReport()
</source>
+
</syntaxhighlight>
=== <translate>Return values</translate> ===
+
 
'''<translate>Returns</translate>''' (table) <translate>table with profiling data</translate><br/>
+
Each table in the array is indexed by a unique function id and has the following content:
 +
*name - Name of the function
 +
*time - Total time spent in the function (seconds)
 +
*count - Number of times this function was called
 +
*callers - A table containing the same information has above for each function which called this one
 +
 
 +
=== Return values ===
 +
'''Returns''' (table) table with profiling data<br/>
 +
 
 +
=== Example ===
 +
<syntaxhighlight lang="lua">
 +
max = 10000
 +
calls = 10
 +
a = -458
 +
 
 +
local abs=math.abs
 +
 
 +
function test1()
 +
for loop=1,max do
 +
local sign=math.abs(a)/a
 +
end
 +
end
 +
 
 +
function test2()
 +
for loop=1,max do
 +
local sign=1
 +
if a<0 then sign=-1 end
 +
end
 +
end
 +
 
 +
function test3()
 +
for loop=1,max do
 +
local sign=a < 0 and -1 or 1
 +
end
 +
end
 +
 
 +
Core.profilerReset()
 +
Core.profilerStart()
 +
for loop = 1, calls do
 +
test1()
 +
test2()
 +
test3()
 +
end
 +
Core.profilerStop()
 +
 
 +
-- print the results of the profiler
 +
result = Core.profilerReport()
 +
print("Number of tests:", max*calls)
 +
for k,v in pairs(result) do
 +
local found=false
 +
for k2,v2 in pairs(v) do
 +
if found and k2=="time" then print(v1,v2) end
 +
if k2=="name" and string.sub(v2,1,4)=="test" then v1=v2 found=true end
 +
end
 +
end
 +
</syntaxhighlight>
 +
 
 +
{{Core}}

Latest revision as of 15:26, 13 July 2023

Available since: Gideros 2017.8
Class: Core

Description

Returns an associative array of tables describing CPU usage of lua functions.

(table) = Core.profilerReport()

Each table in the array is indexed by a unique function id and has the following content:

  • name - Name of the function
  • time - Total time spent in the function (seconds)
  • count - Number of times this function was called
  • callers - A table containing the same information has above for each function which called this one

Return values

Returns (table) table with profiling data

Example

max = 10000
calls = 10
a = -458

local abs=math.abs

function test1()
	for loop=1,max do
		local sign=math.abs(a)/a
	end
end

function test2()
	for loop=1,max do
		local sign=1
		if a<0 then sign=-1 end
	end
end

function test3()
	for loop=1,max do
		local sign=a < 0 and -1 or 1
	end
end

Core.profilerReset()
Core.profilerStart()
for loop = 1, calls do
	test1()
	test2()
	test3()
end
Core.profilerStop()

-- print the results of the profiler
result = Core.profilerReport()
print("Number of tests:", max*calls)
for k,v in pairs(result) do
	local found=false
	for k2,v2 in pairs(v) do
		if found and k2=="time" then print(v1,v2) end
		if k2=="name" and string.sub(v2,1,4)=="test" then v1=v2 found=true end
	end
end