I am looking into generating a file ( 750 MB ) full of random bytes. The code I am using in a separate thread looks like this:
I allocated a buffer of that size since writing on the disk consumes more time:
function Generate(buf:Pointer):DWORD;stdcall;
var
i:DWORD;
begin
for i := 0 to keysize -1 do
PByte(DWORD(buf) + i)^ := Random(256);
Result:=0;
end;
The problem is that it takes ages until the process completes. Any ideas for a faster method? I will try to implement it in assembly if there isn’t any alternative.
This sounded like a nice practice problem so I went ahead and implemented a parallel solution. It uses slightly over 3 seconds to generate 750 MB file and uses over 90% CPU during its work. (SSD disk helps, too. 3,5 seconds were needed to generate the file on a RAID0 disk pair and 4 seconds to generate a file on a slower 512 GB disk.)
All reused code is available with the OpenBSD license (which is almost “use however you wish”): DSiWin32, GpStuff, GpRandomGen, Otl*.
EDIT: Using ForEach in this case was not really elegant solution so I enhanced OmniThreadLibrary with Parallel.ParallelTask and with better IOmniCounter. Using release 993 (or newer) from the SVN you can solve this multiple-producer-single-consumer problem as follows.
EDIT2: A longer blog post about this problem: Life after 2.1: Parallel data production (Introducing Parallel.Task)