简体   繁体   English

第一个RandomAccessFile初始化缓慢,随后很快

[英]First RandomAccessFile initialization slow, subsequently fast

I've noticed that the first initialization of a RandomAccessFile object in Java is much slower than subsequent initializations of RandomAccessFile objects to the same file on disk. 我注意到,Java中RandomAccessFile对象的第一次初始化比随后将RandomAccessFile对象初始化为磁盘上的同一文件要慢得多。 Is there some background caching that the OS does to make this possible? 是否有一些后台缓存操作系统可以实现这一点?

Case in point: I'm loading images from disk and allowing the user to flip through them. 例证:我正在从磁盘加载图像并允许用户翻阅它们。 I was hoping that the bottleneck would be the display of the images, but on first load, the bottleneck was loading of the images (bottleneck was found using JProfiler; RandomAccessFile<Init> ~8ms per call). 我希望瓶颈是图像的显示,但在第一次加载时,瓶颈是加载图像(使用JProfiler发现瓶颈;每次调用RandomAccessFile<Init> ~8ms)。 If I flipped back through images I'd already viewed, the calls to RandomAccessFile<Init> was only several microseconds. 如果我翻回我已经查看的图像,对RandomAccessFile<Init>的调用只有几微秒。

Has anyone ever seen something like this? 有没有人见过这样的东西? Are there any workarounds? 有没有解决方法? A dataset may contain 100,000's of images, so initializing a bunch of dummy RandomAcessFile objects may not be feasible. 数据集可能包含100,000个图像,因此初始化一堆虚拟RandomAcessFile对象可能不可行。

The line of code for initialization is simply: 初始化的代码行很简单:

RandomAccessFile fileIn = new RandomAccessFile(abspath, "r");

Yes, the OS caches. 是的,操作系统缓存。

If you bypass the OS caching, the subsequent opens of a file will be as slow as the first one, so why would you want that? 如果绕过操作系统缓存,后续打开的文件将与第一个文件一样慢,那么为什么要这样呢?

Caching is not slowing the first time you open a file, it's improving the performance of re-opening the file, by not having to wait for the slow harddrive to read the data. 第一次打开文件时缓存不会减慢,它可以提高重新打开文件的性能,而不必等待慢速硬盘驱动器读取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM