 |
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
panraven Grandmaster Cheater
Reputation: 61
Joined: 01 Oct 2008 Posts: 957
|
Posted: Thu Nov 16, 2023 7:37 am Post subject: custom printer with faster luaEngine 'print' |
|
|
The motivation of this extension is to speed up ce's normal 'print', which will output to the LuaEngine UI form.
The main function is 'new_printer(printer, sPrint)' where 'sPrint' is a function to convert inputs into some formatted string, then 'printer' is the function to output the string.
Each call of 'new_printer' will create a new function that equivalent to normal 'print', but may has differnet format, and different output target (for instance, it can be a memo in your custom form).
For 'sPrint' function, the extension provide 2 formation function, you may try make your own formation, eg. to expand/serialize table content ;
Code: |
for input ('a',1,nil,true), the sample outputs are
normal : a 1
num_sprt : [1] a, [2] 1, [3] nil, [4] true
sprt : a, 1, nil, true
|
test code:
Code: |
--[[
local oo = {}
local cc = 1
for ii =1,4 do
cc = cc * 10
local str = 'abc'
local now1 = os.clock()
math.randomseed(1)
for i=1,cc do
prt(i,str:rep(math.random(10,30),', '))
end
now1 = os.clock()-now1
local now2 = os.clock()
math.randomseed(1)
for i=1,cc do
print(i,str:rep(math.random(10,30),', '))
end
now2 = os.clock()-now2
oo[1+#oo] = string.format('%8d:- prt:%10.4f, print:%10.4f, %6.2f%% ', cc, now1, now2, now2/now1*100)
end
createTimer(5000,print, table.concat(oo,'\n\r'))
--]]
--[[ sample result
10:- prt: 0.0090, print: 0.0800, 888.88%
100:- prt: 0.0760, print: 1.1080, 1457.89%
1000:- prt: 0.8600, print: 12.0580, 1402.09%
10000:- prt: 9.4840, print: 119.8530, 1263.73%
--]]
|
Other global:
- original_print : as named, the ce's original 'print' function; in case original 'print' changed, it can be restore with this variable;
- sprt : 'sPrint' formator to split items with tab;
- num_sprt : 'sPrint' formator to prefix items with the order number [1],[2], ... etc
- lua_engine_printer : 'printer' to speed up normal 'print';
- luaEngineprint : the default 'print' function with printer = lua_engine_printer and sPrint = num_sprt
- prt : alias of luaEngineprint
The script has not change the original 'print'.
GOOD:
- faster print using the default 'prt'
- show some variable that may be invincible in normal 'print'
BAD:
- the output of 'new_printer' function has a delay (max 1sec idle) to print;
- there can be make multiple 'print' to output to same target (a memo in a form), but since there is a delay, their mixed output may be out of order, eg.
'prt' and normal 'print' output to the same LuaEngine mOutput ui;
- unlike the default print this is not threadsafe/selfsynchronizing. Do not use outside of the main thread
Save this lua src into a *.lua file in ce's autorun directory
Code: |
local function to_nvar(...)return select('#', ...), ... end
original_print = original_print or print
function sprt(...)
local n, fs, tab = select('#',...), '%s',',\t'
return string.format(fs:rep(n, tab), ...)
end
local function fmap_aux(fn, n, nxt, ...)
if n>0 then return fn(nxt), fmap_aux(fn, n-1, ...)end
end
local function fmap(fn,...)return fmap_aux(fn, to_nvar(...))end
local function fmap_idx_aux(fn, i, n, nxt, ...)
if n>0 then return i, fn(nxt), fmap_idx_aux(fn, i+1, n-1, ...)end
end
local function fmap_idx(fn,...)return fmap_idx_aux(fn, 1, to_nvar(...))end
local function iden(...)return ... end
function num_sprt(...)
local n, fs, tab = select('#',...), '[%d] %s',', '
return string.format(fs:rep(n, tab), fmap_idx(iden, ...))
end
function lua_engine_printer(str)
str = type(str)=='string' and str or ''
local ls = getLuaEngine().mOutput.Lines
ls.beginUpdate()
ls.Text = ls.Count>0 and table.concat({ls.Text, str}, '\n') or str
ls.endUpdate()
if ls.Count > 0 then -- to update displayed
local last_idx = ls.Count - 1
local last_line = ls.getString(last_idx)
ls.delete(last_idx)
original_print(last_line)
else
original_print''
ls.delete(0)
end
end
function new_printer(printer, sPrint)
printer, sPrint = printer or original_print, sPrint or num_sprt
local tmr, buf, cnt, max, nxt, sum = synchronize(createTimer), {}, 1, 500, os.clock(), 0
local function flush()
sum = 0, printer(table.concat(buf, '\n'))
for i=cnt,1,-1 do buf[i]=nil end
cnt = 1
end
tmr.Interval, tmr.Enabled, tmr.OnTimer = 500, true, function(tm)
if cnt > 1 and os.clock() > nxt or cnt > max then flush()end
end
return function(...)
local str = sPrint(...) or ''
sum = sum + str:len()
cnt, buf[cnt], nxt = cnt + 1, str, os.clock() + 1
getLuaEngine().Caption = string.format('Lua Engine / %d / %d',cnt-1, sum)
end
end
luaEngineprint = new_printer(lua_engine_printer, num_sprt)
prt = luaEngineprint
|
_________________
- Retarded. |
|
Back to top |
|
 |
daspamer Grandmaster Cheater Supreme
Reputation: 54
Joined: 13 Sep 2011 Posts: 1588
|
Posted: Tue Jan 21, 2025 9:28 pm Post subject: |
|
|
Small optimization Smile
Code:
Code: | local fmt = (function() return string.format end)(); -- new ptr
local cnct = (function() return table.concat end)();
local gle = (function() return getLuaEngine(); end)();
local clk = (function() return os.clock; end)()
local function to_nvar(...)return select('#', ...), ... end
original_print = original_print or print
function sprt(...)
local n, fs, tab = select('#',...), '%s',',\t'
return fmt(fs:rep(n, tab), ...)
end
local function fmap_aux(fn, n, nxt, ...)
if n>0 then return fn(nxt), fmap_aux(fn, n-1, ...)end
end
local function fmap(fn,...)return fmap_aux(fn, to_nvar(...))end
local function fmap_idx_aux(fn, i, n, nxt, ...)
if n>0 then return i, fn(nxt), fmap_idx_aux(fn, i+1, n-1, ...)end
end
local function fmap_idx(fn,...)return fmap_idx_aux(fn, 1, to_nvar(...))end
local function iden(...)return ... end
function num_sprt(...)
local n, fs, tab = select('#',...), '[%d] %s',', '
return fmt(fs:rep(n, tab), fmap_idx(iden, ...))
end
local ls = gle.mOutput.Lines
function lua_engine_printer(str)
str = type(str)=='string' and str or ''
if ls.count<0 then
original_print''
ls.delete(0)
return;
end
ls.beginUpdate();
ls.Text = ls.Count>0 and cnct({ls.Text, str}, '\n') or str
ls.endUpdate();
local last_idx = ls.Count - 1
local last_line = ls.getString(last_idx)
ls.delete(last_idx)
original_print(last_line)
-- ls.beginUpdate()
-- ls.Text = ls.Count>0 and table.concat({ls.Text, str}, '\n') or str
-- ls.endUpdate()
-- if ls.Count > 0 then -- to update displayed
-- local last_idx = ls.Count - 1
-- local last_line = ls.getString(last_idx)
-- ls.delete(last_idx)
-- original_print(last_line)
-- else
-- original_print''
-- ls.delete(0)
-- end
end
function new_printer(printer, sPrint)
printer, sPrint = printer or original_print, sPrint or num_sprt
local tmr, buf, cnt, max, nxt, sum = synchronize(createTimer), {}, 1, 500, clk(), 0
local function flush()
sum = 0, printer(table.concat(buf, '\n'))
for i=cnt,1,-1 do buf[i]=nil end
cnt = 1
end
tmr.Interval, tmr.Enabled, tmr.OnTimer = 500, true, function(tm)
if cnt > 1 and clk() > nxt or cnt > max then flush()end
end
return function(...)
local str = sPrint(...) or ''
sum = sum + str:len()
cnt, buf[cnt], nxt = cnt + 1, str, os.clock() + 1
gle.Caption = fmt('Lua Engine / %d / %d',cnt-1, sum)
end
end
luaEngineprint = new_printer(lua_engine_printer, num_sprt)
prt = luaEngineprint |
Benchmark:
(optimized script is on right).
https://prnt.sc/gckGwLMkc8dP
(no new ptr)
https://prnt.sc/4d5l4gIcJ6xZ
(6 250 000 calls with this extension)
https://prnt.sc/im2wLu3gR4Gt
(on random, no new ptr)
https://prnt.sc/WgfslVDsfft3
https://prnt.sc/agiL2aaW1PMn
_________________
I'm rusty and getting older, help me re-learn lua. |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|