我有以下代码:
procedure p1(const s:string);
var i,l:integer;
function skip:boolean; //inline not possible
begin
while (i<=l) and (s[i]<=' ') do inc(i);
result:=i<=l;
end;
begin
//skip() is VERY often called here
end;
procedure p2(const s:string);
function skip(const s:string;var i:integer;l:integer):boolean;inline;
begin
while (i<=l) and (s[i]<=' ') do inc(i);
result:=i<=l;
end;
var i,l:integer;
begin
//skip(s,i,l) is VERY often called here
end;
你更喜欢哪一个?第一个可读性更好,但速度较慢,因为 skip() 不能内联。第二种更快,但很丑,因为每次都必须指定所有参数。你知道另一个好的可读和快速的解决方案吗?