Subject:
|
Re: how does a line ends?
|
Newsgroups:
|
lugnet.cad
|
Date:
|
Sun, 8 Apr 2007 09:15:35 GMT
|
Viewed:
|
1744 times
|
| |
| |
Travis Cobbs wrote:
>
> 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.
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 something in that variable will also take time (see
below for better ways).
You might also win a bit of speed by using a switch instead of the
if-statments. Switch statements are usually well optimised by the compiler.
Having one case for '\r' first, and another case for '\n' first will also
eliminate some more of the if-statements (mis-predicted branching is
expensive on todays CPU:s).
But you would probably get the best performance by opening the file in
binary mode, reading full disk blocks into a buffer, and implement MyFgets
on top of that. No library calls, ungetc() is only a Ptr--; and so on.
--
Anders Isaksson, Sweden
BlockCAD: http://web.telia.com/~u16122508/proglego.htm
Gallery: http://web.telia.com/~u16122508/gallery/index.htm
|
|
Message has 1 Reply: | | Re: how does a line ends?
|
| (...) I thought about using a static instead of ungetc() in the first version of readLine() that I posted. This means that you can only read from one open file at a time, but that is usually an OK restriction as long as the programmer is aware of (...) (18 years ago, 8-Apr-07, to lugnet.cad, FTX)
|
Message is in Reply To:
| | Re: how does a line ends?
|
| (...) 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)) bufi = 0; if (i > 0) return buf; else return NULL; if (char1 == (...) (18 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
|
|
|
|