For a 1 MB input file, the execution times in seconds of the programs are:
intro1 6.9 intro2 0.9 intro3 0.4
Buffering
Approaches 2 and 3 use the technique of buffering, where large chunks of a file are read from disk, and then accessed a byte or character at a time. Buffering is a basic and important technique for speeding I/O, and several Java classes support buffering (BufferedInputStream
for bytes,BufferedReader
for characters).
int len = (int)(new File(args[0]).length());
byte buf[] = new byte[len];
This approach is convenient, in that a file can be treated as an array of bytes. But there's an obvious problem of possibly not having enough memory to read in a very large file.
沒有留言:
張貼留言