Code: Select all
local ffi = require("ffi")
local C = ffi.C
local ec = ffi.cast("struct PluginStartupInfo*", far.CPluginStartupInfo()).EditorControl
local egs = ffi.new("struct EditorGetString")
egs.StructSize = ffi.sizeof(egs)
local ess = ffi.new("struct EditorSetString")
ess.StructSize = ffi.sizeof(ess)
local IsSpace
IsSpace = function(char)
return char == 32 or char == 9
end
local KillSpaces1
KillSpaces1 = function(id, lineno)
egs.StringNumber = lineno - 1
if (ec(id, "ECTL_GETSTRING", 0, egs)) ~= 0 then
ess.StringNumber = egs.StringNumber
ess.StringLength = egs.StringLength
ess.StringText = egs.StringText
ess.StringEOL = egs.StringEOL
while ess.StringLength > 0 do
if not IsSpace(ess.StringText[ess.StringLength - 1]) then
break
end
ess.StringLength = ess.StringLength - 1
end
if ess.StringLength ~= egs.StringLength then
return ec(id, "ECTL_SETSTRING", 0, ess)
end
end
end
local function KillSpaces(eid,lineno)
local str, eol = editor.GetString(eid,lineno,3);
local trm = mf.trim(str,2);
if trm and str~=trm then
editor.SetString(eid,lineno,trm,eol);
end;
end
local limit = 10 --seconds
local function bench(f)
local ei = editor.GetInfo()
local eid = ei.EditorID
local start = far.FarClock()
local goal = start+limit*10^6
local c = 0
for i=1,ei.TotalLines do
c=c+1
f(eid,i)
if c==1000 or i==ei.TotalLines then
local time = far.FarClock()
if time>goal or i==ei.TotalLines then
far.Show(math.ceil(i/1000),math.ceil((time-start)/10^6))
return
end
c=0
end
end;
end
MenuItem{-- description="";
guid="AC94873C-C652-4D04-AB1E-DE4DF4EFB455";
menu="Plugins";
area="Editor";
text="~bench~KillSpaces";
action=function()
bench(KillSpaces)
end;
}
MenuItem{-- description="";
guid="25E1ED12-6E51-430B-9E98-4643E5EDB1BF";
menu="Plugins";
area="Editor";
text="~bench~KillSpaces1";
action=function(OpenFrom,Item)
bench(KillSpaces1)
end;
}
Выводит сколько тысяч строк обработано за заданный промежуток времени, ограничение выставлено в 10 секунд.
У меня получилось что разница в производительности - 30 раз
И тормозит вовсе даже не trim..