On Tue, 06 Jan 2004 18:01:29 +0000, Arnold wrote:
>
> "Martijn Lievaart" <> wrote in message
> news
rt.rtij.nl...
>> On Tue, 06 Jan 2004 08:10:52 +0000, Arnold wrote:
>>
>> > Once I get the file into the buffer, I can then do a loop where I pass
> 512
>> > elements of the array to a function until all 9000 elements are
> processed. I
>> > hope that's right. Any other tips on improving speed and efficiency
> would be
>> > appreciated. Thanks.
>>
>> As an alternative to the mmap solution from Glanni, the easiest way to do
>> this would be to read 512 words, process them, write back result, repaet
>> until end-of-file. No need to read the whole file in memory.
>
> I thought of that but speed is a concern so I want to keep the number of
> disk accesses at a minimum.
Memory mapping the file is probably still the best way, but suffers of a
size limit. To get around this, you can also read in large chunks of the
file. Instead of 512 words, read a few 100KB at the time and operate on
that. Experiment with buffer sizes to see what gives the best result.
I'm not sure what will be faster. Large buffers reduce the number of
system calls slightly (good), but decrease locality of reference (bad).
The mmap solution does not suffer either of these disadvantages I think.
Note that the number of disk accesses will be the same whatever solution
you chose. You have to read the whole file, period. I guess the main speed
factors are the number of system calls and how effectively you use your
memory. Also, you should try to do some useful work while waiting for the
disk, maybe asynchronous I/O or multithreading can be of help?
(If you look into multithreading, be sure you know what synchronisation
machisms are lightweight and which are heavyweight, huge difference).
I would just try a simple solution. If it isn't fast enough, try others.
Profile to see where your program spends its time. If most of the time is
spend on calculations, all of the above will give only very marginal
speedups. If run on a fast machine, maybe a naive implementation will be
fast enough for your needs. Remember the old truism about optimizing:
Don't (until you have proven you need it).
HTH,
M4