File Copy in Java – Benchmark
Join the DZone community and get the full member experience.Join For Free
Yesterday I wondered if the copyFile method in JTheque Utils was the best method or if I need to change. So I decided to do a benchmark.
So I searched all the methods to copy a File in Java, even the bad ones and found the following methods :
- Naive Streams Copy : Open two streams, one to read, one to write and transfer the content byte by byte.
- Naive Readers Copy : Open two readers, one to read, one to write and transfer the content character by character.
- Buffered Streams Copy : Same as the first but using buffered streams instead of simple streams.
- Buffered Readers Copy : Same as the second but using buffered readers instead of simple readers.
- Custom Buffer Stream Copy : Same as the first but reading the file not byte by byte but using a simple byte array as buffer.
- Custom Buffer Reader Copy : Same as the fifth but using a Reader instead of a stream.
- Custom Buffer Buffered Stream Copy : Same as the fifth but using buffered streams.
- Custom Buffer Buffered Reader Copy : Same as the sixth but using buffered readers.
- NIO Buffer Copy : Using NIO Channel and using a ByteBuffer to make the transfer.
- NIO Transfer Copy : Using NIO Channel and direct transfer from one channel to other.
I think, this is the ten principal methods to copy a file to another file. The different methods are available at the end of the post. Pay attention that the methods with Readers only works with text files because Readers are using character by character reading so it doesn’t work on a binary file like an image. Here I used a buffer size of 4096 bytes. Of course, use a higher value improve the performances of custom buffer strategies.
For the benchmark, I made the tests using different files.
- Little file (5 KB)
- Medium file (50 KB)
- Big file (5 MB)
- Fat file (50 MB)
And I made the tests first using text files and then using binary files. The source file is not on the same hard disk as the target file.
I used a benchmark framework, described here, to make the tests of all the methods. The tests have been made on my personal computer (Ubuntu 10.04 64 bits, Intel Core 2 Duo 3.16 GHz, 6 Go DDR2, SATA Hard Disks).
And after a long time of bench, here are the results :
We see that the method with a simple stream (Naive Streams) is from far the slowest followed by the simple readers methods (Naive Readers). The readers method is a lot faster than the simple stream because FileReader use a buffer internally. To see what happens to the other, here are the same graph but without the first two methods :
The best two versions are the Buffered Streams and Buffered Readers. Here this is because the buffered streams and readers can write the file in only one operation. Here the times are in microseconds, so there is really little differences between the methods. So the results are not really relevant.
Now, let’s test with a bigger file.
We can see that the versions with the Readers are a little slower than the version with the streams. This is because Readers works on character and for every read() operation, a char conversion must be made, and the same conversion must be made on the other side.
Another observation is that the custom buffer strategy is faster than the buffering of the streams and than using custom buffer with a buffered stream or a single stream doesn’t change anything. The same observation can be made using the custom buffer using readers, it’s the same with buffered readers or not. This is logical, because with custom buffer we made 4096 (size of the buffer) times less invocations to the read method and because we ask for a complete buffer we have not a lot of I/O operations. So the buffer of the streams (or the readers) is not useful here. The NIO buffer strategy is almost equivalent to custom buffer. And the direct transfer using NIO is here slower than the custom buffer methods. I think this is because here the cost of invoking native methods in the operating system level is higher than simply the cost of making the file copy.
Here we see that the Naive Readers shows its limit when the file size if growing. So let’s concentrate us on the best methods only, namely, remove the Naive Readers :
Here, it’s now clear that the custom buffer strategy is a better than the simple buffered streams or readers and that using custom buffer and buffered streams is really useful for bigger files. The Custom Buffer Readers method is better than Custom Buffer Streams because FileReader use a buffer internally.
And now, continue with a bigger file :
You can see that it doesn’t take 500 ms to copy a 50 MB file using the custom buffer strategy and that it even doesn’t take 400 ms with the NIO Transfer method. Really quick isn’t it ? We can see that for a big file, the NIO Transfer start to show an advantage, we’ll better see that in the binary file benchmarks. We will directly start with a big file (5 MB) for this benchmark :
So we can make the same conclusion as for the text files, of course, the buffered streams methods is not fast. The other methods are really close.
We see here again that the NIO Transfer is gaining advantages more the files is bigger.
And just for the pleasure, a great file (1.3 GB) :
We see that all the methods are really close, but the NIO Transfer method has an advantage of 500 ms. It’s not negligible.
In conclusion, the NIO Transfer method is the best one for big files but it’s not the fastest for little files (< 5 MB). But the custom buffer strategy (and the NIO Buffer too) are also really fast methods to take files. So perhaps, the best method is a method that make a custom buffer strategy on the little files and a NIO Transfer on the big ones. But it will be interesting to also make the tests on an other computer and operating system.
We can take several rules from this benchmark :
- Never made a copy of file byte by byte (or char by char)
- Prefer a buffer in your side more than in the stream to make less invocations of the read method, but don’t forget the buffer in the side of the streams
- Pay attention to the size of the buffers
- Don’t use char conversion if you only need to tranfer the content of a file
- Don’t hesitate to use channels to make file transfer, it’s the fastest way to make a file transfer
I’ve also made some tests, but not complete, for files in the same hard disk and here the NIO Transfer method is a lot faster than the other. I think this is because on the same disk this method can make better use of the filesystem cache.
I hope this benchmark (and its results) interested you.
Here are the sources of the benchmark : Java Benchmark of File Copy methods
Opinions expressed by DZone contributors are their own.