Java中的File.exists有多昂贵 [英] How expensive is File.exists in Java

查看:184
本文介绍了Java中的File.exists有多昂贵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何 File.exists()的作品。我不太清楚文件系统是如何工作的,所以我也许应该先阅读一下。



但是,要获得一个快速的信息:



调用$ code File.exists()文件系统的一个动作,如果该路径和文件名注册在某些日志中?或者操作系统是否获取目录的内容,然后通过它扫描匹配?



我认为这将是文件系统依赖的,但也许所有文件系统都使用快速方法?



我不是在谈论网络和磁带系统。让它保持为ntfs,extX,zfs,jfs: - )

解决方案

这个操作如果第一次执行完全依赖在文件系统上。这是由操作系统完成的,Java不起任何作用。



在性能方面,在所有情况下都需要读取磁盘。这通常需要8-12 ms。 @Sven指出一些存储可能更慢,但是在性能很重要的情况下这一点相对较少。如果这是一个网络文件系统(通常相对较小,但取决于您的网络延迟),您可能会有额外的延迟。



其他所有操作系统和Java都是非常但是,如果您重复检查文件,则可能不需要磁盘访问,因为信息可以缓存,在这种情况下,操作系统所需的时间和资源。 File.exists()创建的对象中最大的一个(你不会认为它会),然而它会在每次调用时编写文件的名称,从而创建很多对象。如果将File.exists()放在一个紧密的循环中,它每秒可以创建400MB的垃圾。 :(



通过跟踪对文件系统所做的所有更改,日记文件系统的工作方式不同,但不会更改文件系统的读取方式。 >

I am wondering how File.exists() works. I'm not very aware of how filesystems work, so I should maybe start reading there first.

But for a quick pre information:

Is a call to File.exists() a single action for the filesystem, if that path and filename are registered in some journal? Or does the OS get the content of the directory and then scan through it for matches?

I presume this will be filesystem dependant, but maybe all filesystems use the quick approach?

I'm not talking about network and tape systems. Lets keep it to ntfs, extX, zfs, jfs :-)

解决方案

How this operation if performed the first time is entirely dependant on the filesystem. This is done by the OS and Java doesn't play any part.

In terms of performance, a read to a disk is required in all cases. This typically takes 8-12 ms. @Sven points out some storage could slower, but this relatively rare in cases where performance is important. You may have an additional delay if this is a network file system (usually relatively small but it depends on your network latency).

Everything else the OS and Java does is very short by comparison.

However, if you check the file exists repeatedly, a Disk access may not be required as the information can cached, in this case the time the OS takes and resources. One of the largest of these the objects File.exists() creates (you wouldn't think it would) however it encodes the file's name on every call creating a lot of objects. If you put File.exists() in a tight loop it can create 400MB of garbage per second. :(

Journaling filesystems work differently by keeping track of all the changes you make to a file system, however they don't change how you read the filesystem.

这篇关于Java中的File.exists有多昂贵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆