Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's more efficient read file once, read file when needed? 1

Status
Not open for further replies.

strangeryet

Programmer
Jul 8, 2004
159
US
I have a .txt file that I need to read and grab data from certain lines in the file.

I may need to do this just a few times during processing or perhaps a dozen times.

Is it better to open the file once and build an array of data with the information I might need....

OR
Is it better to open the file, extract the information, close the file, each time I need to get the data?
Thanks!
 
Of course, caching the file once in memory is better.

See thread222-1659145 where we discussed this topic recently.
 
if this is somewhat complex data (multiple columns, records), you may consider using a recordset object to hold the data instead of working with arrays. This will then allow you to do a lot of data searches and manipulation. With ADO you can build a diconnected recordset.

 
It sounds like the text file is being used as a read only lookup. That can be quick without enhancement especially if the buffer specified (Len=intN) is large when the file is Opened for Input. Consider that the file or a large chunk of it may be cached routinely by the Windows disk cache when it is first opened.
It may be quicker to retrieve it from memory but if it is already in memory you may actually suffer from doing it twice; I recommend some timing tests before committing extra memory to the task.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top