Operating with files

R

Ron

I have to compute a file, reading sequentially one character at time. Is it
more convenient (in speed terms) use getc() or copy first the whole file in
a memory buffer and operate with it?

Greetings.
 
J

Joona I Palaste

Ron said:
I have to compute a file, reading sequentially one character at time. Is it
more convenient (in speed terms) use getc() or copy first the whole file in
a memory buffer and operate with it?

The ISO C standard does not specify anything at all about speed or
efficiency. A fully conforming C implementation is even allowed to
*deliberately* waste its time, twiddling its proverbial thumbs, every
time you call some function.
That aside, in *most* implementations, memory operations outperform
stream I/O functions by far. So I would recommend you to first read as
much as you can, and then write it all at once. Doing it byte-by-byte
could be much slower.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
and vow excitedly zooming."
- JIPsoft
 
C

CBFalconer

Ron said:
I have to compute a file, reading sequentially one character at
time. Is it more convenient (in speed terms) use getc() or copy
first the whole file in a memory buffer and operate with it?

Nothing is specified, so you are left with probabilities. Most
file systems have their own buffers, which are already setup to
maximize efficiency, and getc (or getchar) is likely to be a macro
operating on that. Thus using them will minimize both copying and
memory usage.

It is certainly more convenient to use getc in terms of presenting
that single char at a time.

So, all in all, I recommend not making any large copies.
 
T

The real OS2 guy

I have to compute a file, reading sequentially one character at time. Is it
more convenient (in speed terms) use getc() or copy first the whole file in
a memory buffer and operate with it?

Make things so simple as possible! When you have to read a stream char
by char - do so! The system your implementation is running under uses
itself a buffer to handle the I/O primitives the most performant way.
Your implementation uses internally a buffer too, so why does you mean
you have to create yourself another layer of buffers? You makes things
only more complicate than necessary.

O.k., you may play around with setvbuf() but in most cases that will
do nothing for performance anyway.

Use getc() instead of fgetc() and hope that your implementation will
realise getc as macro, so you saves the overhead of a call when the
implementation internal buffer is not empty.

--
Tschau/Bye

Herbert Rosenau
http://www.pc-rosenau.de eComStation Reseller in Germany
eCS 1.1 german is in beta testing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,567
Members
47,204
Latest member
abhinav72673

Latest Threads

Top