Main Menu

KB#00628-How to improve READ/WRITE time to an ASCII file

Title:

How to improve READ/WRITE time to an ASCII file

Description:

When reading an ASCII file, PRO/5 reads one byte at a time. This method isn't very fast, and can cause programs that rely on ASCII files to run very slowly.

Resolution:

The best way to increase speed is to implement the SIZ mode on the IO statements. This way, you can read and/or write much larger chunks of data which will make a dramatic improvement. It's usually typical to use a large SIZ value such as 512 or 1024. 

One important consideration is that your code will have to change to accommodate this feature. This is best demonstrated by an example: 

The ASCII File: 
one 
two 
three 

The old P5 program: 
read(1)a$ 

The new P5 program: 
read record(1,siz=512)a$ 

With the old program, P5 would read a byte at a time until it hit a line terminator. Therefore, the value of a$ would be "one". However, the new way of doing it will read in up to 512 bytes. Because the file is less than 512 bytes, the entire contents of the file are loaded into a$. That means that a$ has all three 'records' and their terminators in it. Therefore, it's necessary to add a parsing algorithm to your code to separate the records from one another. The other thing is that it's very likely that you'll end up with a partial record at the end of your variable. For example, if the read record used a SIZ of 5, then a$ would be the full record "one" (3 bytes), the line terminator (1 byte), and the first letter of the second record - "t" (one more byte). This may seem like a big problem, but can be easily addressed by a good parsing routine. 

Here's a good example provided by Falk Spitzberg: 

        open(1)"Test.dat" 
        loop: 
        gosub get_rec 
        if eof = 1 
                 then stop 
        gosub process_rec 
        goto loop 

        get_rec: 
        Tmp = pos($0a$=buffer$) 
        if Tmp 
                 then rec$ = buffer$(1,tmp-1),buffer$=Buffer$(Tmp+1); return 
        readrecord(1,siz=1024,end=all_done)buffer$ 
        goto get_rec 

        all_done: 
        eof = 1 
        return 

This example uses the GET_REC subroutine to parse out a full record if there is one, and to read more information from the file if the variable doesn't have a full record. 



Last Modified: 02/23/2004 Product: PRO/5 Operating System: N/A

BASIS structures five components of their technology into the BBx Generations.

  Google+ View BASIS LinkedIN Profile Visit our Twitter Feed Check out our Facebook Public Profile Click to View the BASIS youTube channel