BBS: Inland Empire Archive Date: 04-13-92 (14:57) Number: 154 From: DOUGLAS LUSHER Refer#: NONE To: ASA ROSSOFF Recvd: NO Subj: INDEXING... Conf: (2) Quik_Bas
On 04-07-92 at 10:15:10, ASA ROSSOFF wrote: >I wrote a program to display quotes from a file.... But >it's very slow the >first time it's run, because it creates an index file... I >see programs written >in other languages creating similar index files in >seconds, while my program >takes about 20 minutes.... (for a 360k quote file) > >Here's my code to create the index file... any ideas or >suggestions would be >GREATLY appreciated... > >COOKIE.TXT is simply a text file, and a blank line >designates the beginning of >a new cookie... > ...a *bunch* of code follows... Asa, I'm not positive I know what you're doing here, but if you're doing what I think you're doing, you're sure doing it the hard way. I would suggest that you get your QuickBASIC manual and check out the SEEK function and statement. If I understand you correctly, you're writing an index file that is a list of long integer pointers that point to the beginning of the records in your COOKIE.TXT file. If that is the case, you can accomplish the same thing much easier and faster as follows: ''' DIM RecordPointer& BufferLength% = 2 ^ 12 'set this even higher if you can, the more the better OPEN "cookie.idx" FOR RANDOM ACCESS WRITE LOCK READ WRITE AS #1_ LEN = LEN(RecordPointer&) OPEN "cookie.txt" FOR INPUT AS #2 LEN = BufferLength% RecordPointer& = 1 PUT #1, , RecordPointer& 'assume first record starts at the beginning DO UNTIL EOF(2) LINE INPUT #2, L$ IF LEN(L$) = 0 THEN 'a blank line means the end of the record IF NOT EOF(2) THEN 'but we're not at the end of the file RecordPointer& = SEEK(2) 'SEEK() returns the next position in the file PUT #1, , RecordPointer& 'so save it in the index file END IF END IF LOOP CLOSE 1, 2 **** *note* I have not tested the above code, but I think it will work. Now, to get at any particular record: OPEN "cookie.idx" FOR RANDOM AS #1 LEN = LEN(RecordPointer&) GET #1, DesiredRecordNumber%, RecordPointer& CLOSE 1 OPEN "cookie.txt" FOR INPUT AS #2 LEN = BufferLength% SEEK #2, RecordPointer& 'point to the record with the SEEK statement DO LINE INPUT #2, L$ 'grab the record PRINT L$ LOOP UNTIL LEN(L$) = 0 'keep grabbing until you hit a blank line CLOSE 2 ***** *if this is what you are attempting to do, then this should be much simpler and faster, 'though there are some ways this could be speeded up still more, I've just given you this much so make clear the usefulness of SEEK() and SEEK. If this is not what you are trying to do just ignore me! Best regards, Doug --- TMail v1.28 * Origin: TC-AMS MLTBBS 2.2 - Minnetonka, MN (612)-938-4799 (1:282/7)
Books at Amazon:
Back to BASIC: The History, Corruption, and Future of the Language
Hackers: Heroes of the Computer Revolution (including Tiny BASIC)
Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution
The Advent of the Algorithm: The Idea that Rules the World
Moths in the Machine: The Power and Perils of Programming
Mastering Visual Basic .NET