0

I want to know which one is faster. Sequence input stream or file input stream. Here is my sample program

FileInputStream fileInput = new FileInputStream("C:\\Eclipse\\File Output Stream.txt");
FileInputStream fileInput1=new FileInputStream("C:\\Eclipse\\Buffer Output Stream.txt");
SequenceInputStream sequence=new SequenceInputStream(fileInput,fileInput1);
FileOutputStream outputSequenceStream=new FileOutputStream("C:\\Eclipse\\File OutputSequence Stream.txt");
int i=0;
byte[] b = new byte[10];
long start=System.currentTimeMillis();
//System.out.println(start);
while((i=sequence.read())!=-1){
    //outputSequenceStream.write(i);
    System.out.println(Integer.toBinaryString(i)+"  "+i+"  "+ (char)i);
}
System.out.println(System.currentTimeMillis()-start);
System.out.println("success");
System.out.println("Reading one file after another using file input");

FileInputStream fileInput2 = new FileInputStream("C:\\Eclipse\\File Output Stream.txt");
FileInputStream fileInput3=new FileInputStream("C:\\Eclipse\\Buffer Output Stream.txt");
start=System.currentTimeMillis();
/* Reading first file */
while((i=fileInput2.read())!=-1){
    System.out.println((char)i);
}
/* Reading second file */
while((i=fileInput3.read())!=-1){
    System.out.println((char)i);
}
System.out.println(System.currentTimeMillis()-start);
System.out.println("Success");

File input stream gives me less Number than sequence output stream.So does that mean Sequence is slower than file input stream.If so then why do we use sequence stream instead wouldn't it be better to use file input stream?

JohanLarsson
  • 195
  • 1
  • 7
  • 1
    For the record: you want others to spent their time to help you with your question. So you please spend the 1 minute that is required to properly format/indent your code! Indenting matters, and the lack of proper indenting (together with meaningless variable names) renders your input very much "nobody will want to look at it". – GhostCat Aug 13 '18 at 07:59
  • 1
    And then: please consider accepting helpful answers at some point. It seems that you keep asking questions, but rarely accept answers ... – GhostCat Aug 13 '18 at 08:04
  • If you care for performance, you should not transfer files single byte for single byte at all. Consider `try(FileChannel in1 = FileChannel.open(Paths.get("C:\\Eclipse\\File Output Stream.txt"), StandardOpenOption.READ); FileChannel in2 = FileChannel.open(Paths.get("C:\\Eclipse\\Buffer Output Stream.txt"), StandardOpenOption.READ); FileChannel out = new FileOutputStream(FileDescriptor.out).getChannel()) { in1.transferTo(0, in1.size(), out); in2.transferTo(0, in2.size(), out); }` – Holger Aug 22 '18 at 12:48

1 Answers1

3

The javadoc is pretty clear about the purpose of that class:

A SequenceInputStream represents the logical concatenation of other input streams. It starts out with an ordered collection of input streams and reads from the first one until end of file is reached, whereupon it reads from the second one, and so on, until end of file is reached on the last of the contained input streams.

It is nothing but an abstraction, that allows you to easily "concatenate" multiple input sources.

It shouldn't affect performance at all, in that sense, the "real" answer here is to learn how to properly benchmark java code. See here for starters.

On top of that, you are also forgetting about the operating system. In order to really measure IO performance, you should be using different files (to avoid the OS reading things into memory first, and all subsequent reads going to memory!) You would also have to use files with maybe 100 MB of data, not 10 bits.

In other words: your numbers are simply meaningless, it is therefore not possible to draw any conclusions from them.

GhostCat
  • 137,827
  • 25
  • 176
  • 248