Most efficient way to I/O in Java and C

Basically I am combining two questions into one. Both the languages have multiple ways to take input from users and print output to standard console. I have always used C, now starting Java(tired of reinventing wheel in C). However in case of huge data I/O things are different. I need answers for following cases:-

  1. Fastest way to get and put data.
  2. I/O which puts least effect on the system performance. Sometimes reading and writing huge data take a toll on the system and degrades system performance as a whole, making that one program malicious to rest of programs.

Answers to both of the cases may be different. I couldn’t find satisfying answer to both after some googling. I found about getchar_unlocked() etc. but what about Java and the second case? Hence I am asking it here again

I’m Java coder and I had some problems with Java I/O speed before, but I believe, no special constructs like getchar_unlocked() are needed.

When reading input I’m using

new BufferedReader( ..., MAX_SIZE );

constructor, where I specify biggest file size.

When I’m parsing lines I’m NOT using split() or Scanner but StringTokenizer.

And if there is a lot of output, I use StringBuilder to concatenate partial results and at the end I’m printing this one big String with one System.out.println() call.

2 Likes

I do not know about Java but you might want to see these for C for fast I/O :
link
and link2.

I do not know much about Java but i think fast I/O is achieved in Java by storing all outputs in a string by concatenation and then printing at the end.

1 Like

I read the links, really useful. Thanks!

Both StringBuilder and StringTokenizer seems to be a good addition to my tools :slight_smile:

But I am worried about the 2nd case too for some serious development( not programming contests). Is there anything like block read? So that they wont affect system by raising interrupt for reading each character off the screen?