为什么从Channel构造的InputStream比从FileDescriptor构造的快得多?

时间:2014-05-02 10:10:46

标签: java inputstream fileinputstream channel kryo

当在RandomAccessFile上构造一个InputStream以便让Kryo反序列化它的对象时,无论是通过文件的Channel构建中介的InputStream(提供良好的性能)还是通过其FileDescriptor(给出),它似乎都会对性能产生巨大的影响。可怕的表现):

RandomAccessFile ra = new RandomAccessFile(dataFile, "r");

Input input1 = new InputWithRandomAccessFile(Channels.newInputStream(ra.getChannel()), FILE_BUF_SIZE, ra);
Input input2 = new InputWithRandomAccessFile(new FileInputStream(ra.getFD()), FILE_BUF_SIZE, ra);

InputWithRandomAccessFile是我自己的类,它扩展了Kryo的Input类,只有在调用#setPosition时它在R / A文件中寻找正确位置的唯一附加行为。

从input1读取3,000个固定大小的对象需要大约600毫秒,从输入2大约需要16秒。

0 个答案:

没有答案