To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.cadOpen lugnet.cad in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 CAD / 14523
14522  |  14524
Subject: 
Re: how does a line ends?
Newsgroups: 
lugnet.cad
Date: 
Sat, 7 Apr 2007 18:32:40 GMT
Viewed: 
1657 times
  
In lugnet.cad, Travis Cobbs wrote:
   I’ll tell you what. I’ll drop your algorithm into LDView and do some empirical tests on the timing, and get back to you. I’ll post the final version of my fgets replacement along with the timing results.

OK, as promised, here it my fgets replacement:
char *myFgets(char *buf, int bufSize, FILE *file)
{
    int i;

    for (i = 0; i < bufSize - 1; i++)
    {
        int char1 = fgetc(file);

        if (feof(file))
        {
            buf[i] = 0;
            if (i > 0)
            {
                return buf;
            }
            else
            {
                return NULL;
            }
        }
        if (char1 == '\r' || char1 == '\n')
        {
            int char2 = fgetc(file);

            buf[i] = '\n';
            buf[i + 1] = 0;
            if (!feof(file))
            {
                if (!(char1 == '\r' && char2 == '\n' ||
                    char1 == '\n' && char2 == '\r'))
                {
                    ungetc(char2, file);
                }
            }
            return buf;
        }
        buf[i] = (char)char1;
    }
    buf[bufSize - 1] = 0;
    return buf;
}
It seems to work, but I’m not 100% confident in its lack of bugs. Timing with 8464.mpd results in about 550ms for loading using fgets and 750ms using myFgets from above. On the one hand, thats a 50% slowdown based purely on that one change. On the other hand, 750ms isn’t very long, and while 8464.mpd isn’t exactly a huge file, it’s big enough to prove your point that the performance is fine.

At this point, I’m much more concerned about any possible bugs in the code, though. I have a fairly high confidence level that fgets does what it’s supposed to do in all possible cases. I don’t have the same confidence level for the myFgets function above.

--Travis



Message has 3 Replies:
  Re: how does a line ends?
 
(...) That was quick! Thanks for taking the time to indulge me... (...) It is interesting that it was that much time difference. Are these the total load times, or just profiling of the time spent inside the fgets() routines? I see one or two things (...) (17 years ago, 7-Apr-07, to lugnet.cad, FTX)
  Re: how does a line ends?
 
(...) Just as a proof of the value of disk caching, loading the same model using the system fgets after a fresh reboot, the same loading portion took 3300ms. Also, I switched the feof calls out for a check of the value against EOF just to see if it (...) (17 years ago, 7-Apr-07, to lugnet.cad, FTX)
  Re: how does a line ends?
 
(...) As long as you only have one character to 'unget' you could probably speed it up by introducing a static char which holds the 'ungetted' char (or null), instead of going through ungetc() -- fgetc(). OTOH, the if-statement to check if there is (...) (17 years ago, 8-Apr-07, to lugnet.cad)

Message is in Reply To:
  Re: how does a line ends?
 
(...) You'll note that I didn't really say that there was anything wrong with your parsing routine (other than some personaly negative feelings about fgetc and ungetc). After fixing the bugs, it will do exactly what you say it will do. It's just (...) (17 years ago, 7-Apr-07, to lugnet.cad, FTX)

24 Messages in This Thread:











Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR