I'm trying to write a program which reads a file really small size with two number and a character and ouput another file which can have up to 120.000.000 characters. My answer which is the fastest way to achieve this on Linux compiled with gcc i mean shouls i use the stdio library which library is the fastest ot is there something else idon't know which would make everything much faster.

thanks in advance

The functions in either stdio.h or <fstream> will do the job equally well, but for c++ most programmers would recommend <fstream> for consistency. If all the characters you want to write are all in one large block of RAM than you would write the file in binary mode, which is a lot faster than text mode.

>My answer which is the fastest way to achieve this on Linux compiled with gcc
I'd say skip the standard stuff and drop down to the write function as a start. This removes a potential level of abstraction, but of course you should be profiling all of these options to see which is actually faster in practice. My educated guess is that as far as the vanilla solutions go, iostreams will be the slowest, followed by stdio, then unistd. Though the difference may not be as much as you'd hope.

>you would write the file in binary mode, which is a lot faster than text mode.
The only difference between text and binary mode is character translation. On Linux the two modes are effectively identical, so I would be somewhat surprised if binary is "a lot" faster than text.

>> I would be somewhat surprised if binary is "a lot" faster than text.

What I meant by that is the fwrite() function (binary writing) is a lot faster than fprintf() (text mode writing).

Perhaps start with the realisation that disk seek times are measured in milliseconds, and processor instruction times are measured in nanoseconds (that's 1E6 times quicker).

It's probably nearer 1E7 since fast processors are sub-1-ns and disk seek times are at best 5ns (or worse).

What's 1E7 look like?
Well if you go to a cafe and spend 1 second ordering a coffee, then 1E7 seconds to deliver the coffee is over 3 MONTHS.

Having figured out that 90% of the time has absolutely nothing to do with any code which you write, go with something which is easiest to understand and maintain.

>What I meant by that is the fwrite() function (binary writing)
>is a lot faster than fprintf() (text mode writing).
I see. However, I don't like the distinction you're making. I prefer to think of it as unformatted vs. formatted[1] rather than binary vs. text. This fixes one of the more common misconceptions that, for example, fread/fwrite can't be used on text streams.

[1] The standard separates the I/O functions into formatted (printf, scanf, etc...), character (getchar, putchar, fgets, puts, etc...), and direct (fread and fwrite). Formatted directly supports varying types, character supports characters and strings, and direct supports block I/O through generic pointers.

Comments
good point :)

My educated guess is that as far as the vanilla solutions go, iostreams will be the slowest, followed by stdio, then unistd

ok i see
is unistd equal to direct system call function? i am sure i've read something about that and it's beeing faster but i am not sure. i've made a search but it has been fruitless

This article has been dead for over six months. Start a new discussion instead.