Array question

  • Thread starter Thread starter Steve Farrar
  • Start date Start date
S

Steve Farrar

I have a macro that processes a text file, reading data one line at a time.

Originally I did 2 "passes" on the file, the first step merely established
the number of lines in the file so that I could use a progress indicator on
the next pass through the file where I did my actual processing.

Then I got the bright idea that instead of opening and closing the file
twice I only needed to do it once and could save considerable processing
time. I would open the file, and pass each line to an arrayed variable.
After establishing the mumber of lines I would then access the array data
instead of opening the file again.

To my surprise this ended up saving virtually no time at all. Can anyone
explain this ?
 
Perhaps reading a file is not as inefficient as you thought. If you are
filling the array fully, then reading the array, isn't this like reading the
file twice? I am curious how much time difference there is between just
reading the once vs reading it twice? For small files (< 100's of thousands
lines), I would expect the difference to be barely noticeable.

Bob
 
Yes, in general, disk i/o is slower than memory access, but...

If the file is small enough and the system has enough memory, it could
be caching enough/all of the file...

Or, the file is so large and the array contains so much data that the
virtual memory manager is swapping parts of the array to the
swapdisk...

Or, the sequential read of the file triggers a sufficiently smart 'read
ahead' capability within the IO subsystem that records you access are
always in memory...

Or...

--
Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions
 
Back
Top