Sunday, March 31, 2013

Java Fast Input/Output





Most of the time I write my codes in Java,and its a bit frustrating to see that one cannot attain the speed of execution here compared to the other languages like C,Cpp. However, Java is very feature rich in terms of library support, user base and ease of OOP development.Due to its very organized and huge collection of library its often the programming language of choice for most professionals.

Java being an interpreted language is slower than C and other compiled language ,however it still can be optimized to the level where it can attain runtime speeds which varies from C by a factor of less than 2,and major optimization can be attained within the Input/Output section itself.

Often people use the Scanner class in Java for input(be it reading from a file or console),however Scanner is the slowest possible way of taking an input.

There are two other better class to achieve this :

1)BufferedReader
2)BufferedInputStream 

BufferedReader provides us with automated buffer management and also the very useful readLine() method(can take entire String of line as input,similar to Scanner's nextLine()) method.Often its sufficient when we deal with String inputs to use the buffered Reader class.
Given below is a small code snippet which demonstrates how to used the BufferedReader to take inputs as String.



BufferedInputStream is the fastest way to take Input,however we need to make our own buffer management and also there is no readLine() method predefined.

So,I am going to create  the following using BufferedInputStream.

  •  function readInt() //reads the next Integer skipping the newline and spaces
  •  function readLong() //reads the next Long skipping the newline and spaces
  •  function readString() //reads the next String till it gets a newLine("\n") 


Also,I have used the StringBuilder class here .StringBuffer is supposedly the fastest  way to build and manipulate Strings in java instead of the regular concatenation.However StringBuilder is not threadsafe, and for those of you who wants to use a threadsafe version can instead use the StringBuffer class.
Below I have embedded the code for the custom class I have created and added suitable comments.






Fast Input/Output in C




This is my first post and I would begin it with one of the very crucial and overlooked segment of a program.
i.e the IO(Input/Output).
Most of us are content with using scanf,printf,getc,putchar in C .
For programs with large number of numerical inputs(ints,long) in C we often use scanf("%d") which is even slower than taking input as a String using scanf("%s")  or gets() and then converting the number using atoi function.

Almost everyone has never heard of the function getchar_unlocked()  in C.

  •  getchar_unlocked() is a macro provided by C and this function has much less overhead as compared with the getchar().
  • getchar_unlocked() is not thread safe and does not have many safety checks.Although it is not recommended to use a function which is not thread safe,it can be safely used if we just need to take inputs and don't have a multi threaded program.(usually in the programming competitions where speed is the primary concern).
Below is  the code snippet which demonstrates how to use the getchar_unlocked() for developing fast Input solutions for reading int,long etc.

PS: I have used the inline keyword here,Inlining is done to request the compiler to replace the source code of the function at the places its being called,thereby it will become a bit faster and optimized as there wont be an overhead for allocating the stack separately for the function.



Normally Printf is quite fast for outputs,however for writing Integer or Long Outputs,the below function is a tad bit faster. Here we use the putchar_unlocked() method for outputting a character which is similar thread-unsafe version of putchar() and is faster.