Ibrahim F Haddad wrote:
> I have files of the following formats:
> ------------------------
> Name
> Company
> day
> month
> year
>
> job_name time max min flag1 flag2 flag3 flag4 job_name
> job_name time max min flag1 flag2 flag3 flag4 job_name
> job_name time max min flag1 flag2 flag3 flag4 job_name
> ------------------------------
>
> Explanation of file format:
> ---------------------------
> flag4 determines how many job the current task depends on.
> that is for:
>
> a 12 123 145 0 1 2 3 c v b
> ^
> We know that job a depends on three tasks that are: c, v, and b
> if we have
> b 13 113 125 0 1 2 0
> ^
> then this job does not depend on any other job.
> -----------------------------
> My question:
> ------------
> I have a dynamic structure to hold these info.
>
> It is very important for me to read this file and store the
> info in the structure as fast as possible. I have a front end
> graphical interface for this and I need to know how reading/storing
> in structure can be done very fast so that I can save waiting-time
> for the user when having a file containing a lot of job entries.
The fastest way to read textual data is to write your own scanner
using lex/flex, and to ensure that it doesn't need to back-up (see the
flex info file for what backing-up is and how to avoid it).
As for storing it into the structure, you have several options. Their
relative suitability depends upon whether the dependencies have a
fixed size (in which case, you know how much memory the record
requires once you have read flag4).
a) Read each record into a block of memory which is large enough to
hold any record. This wastes memory, but avoids copying or
reallocating the block.
b) Read each record into a temporary buffer that is large enough to
hold any record, and then copy the record to a malloc()d block once
you know how large it is.
c) First, read everything up to and including flag4 into a temporary
buffer. Then allocate the actual buffer, copy the fields that have
already been read into it, and read the dependencies directly into the
actual buffer.
d) Similar to c), but store the dependencies in a separate block, and
store a pointer to that block in the first block.
e) Write the records to a file as they are read in, then mmap() the
file once it's complete. This approach makes storing pointers to
records awkward, although this may not necessarily be a problem.
No doubt there are also other possibilities.
--
Glynn Clements <[EMAIL PROTECTED]>