program bench2;

{$MODE OBJFPC}{$H+}

uses

SysUtils, DateUtils, StrUtils, Str2IntAlter;

var

a: TStringArray = nil;

Trash: TStringArray = (

' --', ' ++', ' +-',' --0', ' ++0', ' +-0',' -0-1', ' +0--1', ' 0-1-1',

'9223372036854775808', '-9223372036854775809');

function NextInt: string;

begin

case Random(4) of

0: Result := ' '#9'-' + Random(High(Int64)).ToString;

1: Result := ' '#9'+' + Random(High(Int64)).ToString;

2: Result := ' '#9'-' + Random(Int64(High(Dword))).ToString;

else

Result := ' '#9'+' + Random(Int64(High(Dword))).ToString;

end;

end;

procedure GenTestData;

var

I: Integer;

const

TestSize = 1000;

begin

SetLength(a, TestSize);

for I := 0 to High(a) do

a[I] := NextInt;

end;

procedure RunVal;

var

I, J, r, c: Integer;

Start: TTime;

v, Score: Int64;

begin

for I := 0 to High(Trash) do

begin

Val(Trash[I], v, c);

if c = 0 then

begin

WriteLn('Val() accepts trash');

exit;

end;

end;

r := 0;

Start := Time;

for J := 1 to 30000 do

for I := 0 to High(a) do

begin

Val(a[I], v, c);

Inc(r, Ord(c <> 0));

end;

Score := MillisecondsBetween(Time, Start);

WriteLn('Val(), score: ', Score);

WriteLn('rejected: ', r);

end;

procedure RunAlt;

var

I, J, r: Integer;

Start: TTime;

v, Score: Int64;

begin

for I := 0 to High(Trash) do

if TryChars2Int(Trash[I][1..Length(Trash[I])], v) then

begin

WriteLn('TryChars2Int accepts trash');

exit;

end;

r := 0;

Start := Time;

for J := 1 to 30000 do

for I := 0 to High(a) do

if not TryChars2Int(a[I][1..Length(a[I])], v) then

Inc(r);

Score := MillisecondsBetween(Time, Start);

WriteLn('TryChars2Int, score: ', Score);

WriteLn('rejected: ', r);

end;

procedure RunAltDec;

var

I, J, r: Integer;

Start: TTime;

v, Score: Int64;

begin

for I := 0 to High(Trash) do

if TryDecimals2Int(Trash[I][1..Length(Trash[I])], v) then

begin

WriteLn('TryDecimals2Int accepts trash');

exit;

end;

r := 0;

Start := Time;

for J := 1 to 30000 do

for I := 0 to High(a) do

if not TryDecimals2Int(a[I][1..Length(a[I])], v) then

Inc(r);

Score := MillisecondsBetween(Time, Start);

WriteLn('TryDecimals2Int, score: ', Score);

WriteLn('rejected: ', r);

end;

begin

Randomize;

GenTestData;

RunVal;

WriteLn;

RunAlt;

WriteLn;

RunAltDec;

WriteLn;

WriteLn('Press any key to exit...');

ReadLn;

end.