試試這個
const MaxString = 15; // you said so
type stringholder = record
strict private
var Cell: string[ MaxString * SizeOf(Char) div SizeOf(AnsiChar) ];
function GetUS: String; // in xe 4 that is a shortcut to UnicodeString actual type
procedure SetUS(const US: string);
public
property Value: string read GetUS write SetUS;
class operator Implicit(const from: string): stringholder; inline;
class operator Implicit(const from: stringholder): string; inline;
end;
function stringholder.GetUS: String;
var i: integer;
begin
i := Ord(Cell[0]);
SetLength(Result, i div (SizeOf(Char) div SizeOf(AnsiChar)));
if i > 0 then
Move(Cell[1], Result[1], i);
end;
procedure SetUS(const US: string);
var i: integer;
begin
If US = '' then begin
Cell := ''; // constant here, not US itself
Exit;
End;
i := Length(US);
If i > MaxString then raise EInvalidCast.Create('.....'+US);
i := i * SizeOf(Char) div SizeOf(AnsiChar)
Move(US[1], Cell[1], i);
Cell(. 0 .) := AnsiChar(i);
end;
class operator stringholder.Implicit(const from: string): stringholder;
begin
Result.Value := from;
end;
class operator stringholder.Implicit(const from: stringholder): string;
begin
Result := from.Value;
end;
const
N = 10000000; // 10 million
M = 10;
var
i,j: integer;
s: string;
X: array of stringholder;
begin
setlength(X,N);
for i:= 1 to N do
begin
s:= '';
for j:= 1 to M do s:= s+chr(65+random(25));
X(. i-1 .) := s;
end;
showmessage('pause');
end;
我的猜測是內存碎片...但它真的只是一種猜測。 – GabrielF
是的,將s:= s +替換爲s [j]:=將其降至420 MB,但仍浪費了很多。 –
如果你做'X:Char'的Array [1..10]數組怎麼辦?它變得更好嗎? – GabrielF