While doing problems in various coding platforms in some questions we end up with (TLE). At that point of time even if we use fast input then also, sometimes (TLE) remains in Java. At that time if we use fast output with fast input it can reduce the time taken. The fast output is generally used when we have to print numerous items which is shown later and discussed below as method 2

Methods:
- Without using fast output(Naive)
- Using fast output (Optimal)
Method 1: Without using fast output (Standard Method)
System.out.println() is used to print an argument that is passed to it. It can be broken into three parts
- System: It is a final class defined in java.lang package.
- out: It is a static member field of System class and is of type PrintStream
- println(): It is a method of PrintStream class. println prints the argument passed to the standard console and a newline.
Implementation: Program in which we are printing the value from 0 to 10^5
Example
Java
// Java Program to Print VeryLarge Bunch of Numbers
// without using fast output concept
// Importing all input output classes
/**
* @author Akhil
*/
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Iterating over very large value
for (int i = 0; i < 100000; i++)
// Print all the elements of an array
// till the condition violates
System.out.println(i);
}
}
Output: 10000 numbers from 0 till 9999 are printed and this program takes 0.88 seconds to execute


Method 2: Using fast output (Optimal)
PrintWriter class is the implementation of Writer class. By using PrintWriter than using System.out.println is preferred when we have to print a lot of items as PrintWriter is faster than the other to print data to the console.
The System.out variable is referencing an object of type PrintStream which wraps a BufferedOutputStream. When you call one of the write() methods on PrintStream, it internally flushes the buffer of the underlying BufferedOutputStream. It doesn't happen with PrintWriter. We have to do it ourselves.
Implementation:
Example
Java
// Java Program to Print VeryLarge Bunch of Numbers
// Importing input output classes
/**
* @author Akhil
*/
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
PrintWriter out=new PrintWriter(System.out);
// Again iterating over very Big value
for (int i = 0; i < 100000; i++)
out.print(i + "\n");
// Flushing the content of the buffer to the
// output stream using out.flush() methods
out.flush();
}
}
Output:
Same output is generated that is 10000 numbers from 0 till 9999 are printed and this program takes 0.14 seconds to execute which is 84% faster output is generated in respect to time comparing the above two examples as illustrated.


Similar Reads
HashMap and TreeMap in Java HashMap and TreeMap are part of collection framework. HashMapjava.util.HashMap class is a Hashing based implementation. In HashMap, we have a key and a value pair<Key, Value>. HashMap<K, V> hmap = new HashMap<K, V>(); Let us consider below example where we have to count occurrences
5 min read
Convert HashSet to TreeSet in Java Hashset: Hashset in Java is generally used for operations like search, insert and delete. It takes constant time for these operations on average. HashSet is faster than TreeSet. HashSet is Implemented using a hash table. TreeSet: TreeSet in Java takes O(log n) for search, insert and delete which is
3 min read
Convert HashSet to TreeSet in Java Hashset: Hashset in Java is generally used for operations like search, insert and delete. It takes constant time for these operations on average. HashSet is faster than TreeSet. HashSet is Implemented using a hash table. TreeSet: TreeSet in Java takes O(log n) for search, insert and delete which is
3 min read
Java IO Tutorial Java programming language comes with a variety of APIs that helps the developers to code more efficiently. One of those APIs is Java IO API. Java IO API helps the users to read and write data. In simple words, we can say that the Java IO helps the users to take the input and produce output based on
15+ min read
Stream.of(T... values) in Java with examples Stream of(T... values) returns a sequential ordered stream whose elements are the specified values. A sequential stream works like a for-loop using a single core. On the other hand, a Parallel stream divides the provided task into many tasks and runs them in different threads, utilizing multiple cor
3 min read
Java Cheat Sheet Java is a programming language and platform that has been widely used since its development by James Gosling in 1991. It follows the Object-oriented Programming concept and can run programs written on any OS platform. Java is a high-level, object-oriented, secure, robust, platform-independent, multi
15+ min read