Answer:
Sometimes we have to read from files by handling byte by byte.
Experience shows that reading byte by byte from a TFileStream or whatever variant that can read files does so slowly.
The cause is simple. The harddisk needs to find the location on the hdd each time you request a read from a file. I have been working with compression and sometimes the programs demanded actual hours to simply read a file...
This problem is solved pretty easily by reading not in bytes but in huger
chunks of data and adapting your programme to this new structure.
eg.
FFile : TFileStream;
bbyte : byte;
buffer : array[0..65535] of byte;
...
FFile.read(bbyte,1); //the slow way to do it, but little complex
FFile.read(buffer,65536);
//reads much faster but handling each byte must be done in some kind of loop.
As you would work with the buffer you would soon see that this method is faster.
On my machine I tested it on 600MB, the byte by byte reading method required
several hours while reading the buffers 13 minutes.
In order to reduce complexity in using this buffer I have made a class
that does all this for you.(making the buffer, reading if the buffer is completely worked through)I call it the TAdvancedMemoryStream.
Implementing this will allow the simplicity to read each and every byte sequentially.
FileReader : TAdvancedFileStream;
b : byte;
b := FileReader.readByte;
The FileReader will automatically use a buffer and due too it's simplicity this
class can only be recommended if you wish to read bytes sequentially and doing so in the fastest and simplest way available.
The .pas file can be downloaded from my site :
www25.brinkster.com/denshade
Mind though that the current version at 27/10/2002 only supports reading bytes and retreiving the position.
If you have remarks or you have found a huge bug in the code you could drop
by a mail at denshade@hotmail.com...even though the chances that i find your mail between all the junkmail is fairly little. My mailbox is set to selective
so if you do try to mail me, mention the component in the title.
I hope this is somehow usefull for someone,
DS
|