954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

I/O by Buffer In C

HI, I am looking for faster input and output method for the C other than just normal scanf and printf modules.

Please elaborate by simple example.

thanks :)

chhabraamit
Newbie Poster
5 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

This is highly system dependent and it is unlikely, even if we knew your system, that a 'simple example' would present itself without a significant amount of work.
I'd suggest you google for I/O optimization for your particular platform.

L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 

OK .. Thanks

chhabraamit
Newbie Poster
5 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 
Please elaborate by simple example.


No,you elaborate first. What kind of I/O? Are you doing type conversions like scanf and printf? Do you know about the unformatted I/O functions? Have you profiled your code and confirmed that C's standard I/O is too slow?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I was meaning to know the buffer I/O method and yeah i have some knowledge of unformatted
methods like gets and puts .

And i havent confirmed but got to know from some sites that standard I/O methods are slower than buffer I/O methods(and method wasn't explained on those sites) as faster I/O methods are required for some competitions where run time allowed is 2-3 sec for 10^6 integer inputs .

chhabraamit
Newbie Poster
5 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 
I was meaning to know the buffer I/O method


Can you specify what you mean by "buffer I/O method"? The standard I/O streams are buffered by default for performance. You can adjust the buffering amount to suit your needs, or even turn it off. Presumably the sites you were reading were talking about using system functions instead of standard C functions, such as POSIX's read() or Windows' ReadFile(). These can potentially be faster, or not, depending on many factors and require thorough testing.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I would guess he means "formatted I/O" instead of buffered I/O. Which would be fprintf/fscanf.
Basically the functions that try to interpret the data that's read to some specific type.
I never tried in C/C++ but in Java it was quite clear that ObjectInputStream.readInt() is much slower than FileInputStream.read().
If I'm wrong Narue's post gives teh correct answer.

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

In DaniWeb terms the undefined behaviour could be translated: risk getting struck by lightning by Goddess Narue ;) if you use it.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 
I never tried in C/C++ but in Java it was quite clear that ObjectInputStream.readInt() is much slower than FileInputStream.read().


It's also generally a safe assumption that reading a string and converting it to another type is slower than simply reading and storing a string.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: