Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program TestString;
- {$APPTYPE CONSOLE}
- uses
- Diagnostics,
- SysUtils;
- {$UNDEF REUSE_STRING}
- {$DEFINE REUSE_STRING}
- type
- TTest = procedure;
- const
- dwBufferLen = 10000;
- var
- lpBufferPos, lpStart: PChar;
- {$IFDEF REUSE_STRING}
- var
- s: string;
- {$ENDIF}
- procedure CallMove;
- var
- len: Integer;
- {$IFNDEF REUSE_STRING}
- s: string;
- {$ENDIF}
- begin
- len := lpBufferPos - lpStart;
- SetLength(s, len);
- Move(lpStart^, s[1], len * SizeOf(Char));
- end;
- procedure CallMoveMod;
- var
- len: Integer;
- {$IFNDEF REUSE_STRING}
- s: string;
- {$ENDIF}
- begin
- len := lpBufferPos - lpStart;
- SetLength(s, len);
- Move(lpStart^, Pointer(s)^, len * SizeOf(Char));
- end;
- procedure CallSetString;
- {$IFNDEF REUSE_STRING}
- var
- s: string;
- {$ENDIF}
- begin
- SetString(s, lpStart, lpBufferPos - lpStart);
- end;
- procedure Test(const test: TTest);
- const
- max = 5000000;
- var
- idx: Integer;
- lpInput: PChar;
- swTimer: TStopwatch;
- begin
- swTimer := TStopwatch.Create();
- lpInput := StrAlloc(dwBufferLen);
- try
- // this will have a distance of 40% of the length of the buffer between
- // the start and the position in the buffer.
- // example:
- // if dwBufferLen is 1000, the copied string will be 400 characters long,
- lpBufferPos := lpInput;
- Inc(lpBufferPos, dwBufferLen div 2);
- lpStart := lpInput;
- Inc(lpStart, dwBufferLen div 10);
- swTimer.Start;
- for idx := 1 to max do
- test();
- swTimer.Stop;
- Writeln('Ticks ', swTimer.ElapsedTicks);
- finally
- StrDispose(lpInput);
- end;
- end;
- begin
- {$IFDEF REUSE_STRING}
- Writeln('output string will be reused');
- Writeln;
- {$ENDIF}
- Writeln('SetLength & Move');
- Test(CallMove);
- // 9396476 with reuse
- // 9918661 without reuse
- Writeln('SetLength & Move modified');
- Test(CallMoveMod);
- // 9368567 with reuse
- // 9866585 without reuse
- Writeln('SetString');
- Test(CallSetString);
- // 9601162 with reuse
- // 9842723 without reuse
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement