Shuffle Text File Delphi Source or anything else

I have a string list with 10,000 entries. I have a regular procedure, but access to any of the elements takes a lot of time. Passing through all 10k elements takes a huge amount of time.

I want to save it to disk and then make shuffle in a file using another method.

Any suggestions?

+3
source share
4 answers

How is your regular shuffle done? Especially the exchange rate? If you wrote your own, follow these steps:

vTempSrting := vStringList[I]; 
vStringList.Delete(I); 
vStringList.Insert(J,vTempString);

he will be very slow. Use the exchange method in the string list.

78 (3-) :

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,Classes,uIntegerList,Windows,Math;

procedure Shuffle(aSL : TStringList);
var I,J : integer;
begin
  for I := 0 to aSL.Count-1 do
  begin
    J := randomrange(I,aSL.Count);
    aSL.Exchange(I,J);
  end;
end;

procedure CreateTestFile;
var
  vSL : TStringList;
  I : integer;
begin
  vSL := TStringList.Create;
  try
    for I := 1 to 100000 do vSL.Add('Sample text #'+inttostr(I));
    vSL.SaveToFile('c:\test.txt');
  finally
    vSL.Free;
  end;
end;

function TestShuffle : longword;
var
  vSL : TStringList;
  vTick0 : longword;
begin
  vSL := TStringList.Create;
  try
    vTick0 := gettickcount;
    vSL.LoadFromFile('c:\test.txt');
    Shuffle(vSL);
    vSL.SaveToFile('c:\test.txt');
    Result := gettickcount - vTick0;
  finally
    vSL.Free;
  end;
end;

begin
  CreateTestFile;
  writeln(TestShuffle,' ms');
  readln;
end.
+7

, .

, . . 10 000 , , , , , .

, , .

+1

- , , . o (n * log (n)), o (n), .

, , , , , .

+1

, - , , , , O (n)

PRNG,

- , , , . - , ( 32 64- ) . , N- , N * 4 ( N * 8 64- ), , .

, , . , , , , ( ) ( , ), , , (, SSD).

10K . - 10 , , ( , ), , ( - ) 32- .

+1

Source: https://habr.com/ru/post/1720664/


All Articles