Java 中的 File.exists 有多贵

How expensive is File.exists in Java(Java 中的 File.exists 有多贵)
本文介绍了Java 中的 File.exists 有多贵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想知道 File.exists() 是如何工作的.我不太了解文件系统的工作原理,所以我应该先从那里开始阅读.

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

但对于快速的预信息:

调用 File.exists() 是否是文件系统的单个操作,如果该路径和文件名已在某个日志中注册?还是操作系统获取目录的内容,然后扫描它以查找匹配项?

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?

我不是在谈论网络和磁带系统.让我们把它保存到 ntfs、extX、zfs、jfs :-)

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

推荐答案

如果第一次执行此操作完全取决于文件系统.这是由操作系统完成的,Java 没有任何作用.

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.

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

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).

相比之下,操作系统和 Java 所做的一切都非常短.

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

但是,如果您反复检查文件是否存在,则可能不需要磁盘访问,因为可以缓存信息,在这种情况下是操作系统占用的时间和资源.File.exists() 创建的这些对象中最大的一个(您不会认为它会创建),但是它会在每次调用创建大量对象时对文件名进行编码.如果你把 File.exists() 放在一个紧密的循环中,它每秒会产生 400MB 的垃圾.:(

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 有多贵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to implement RecyclerView with section header depending on category?(如何根据类别实现带有节标题的 RecyclerView?)
How to generate JNI header file in Eclipse(如何在 Eclipse 中生成 JNI 头文件)
Setting a custom HTTP header dynamically with Spring-WS client(使用 Spring-WS 客户端动态设置自定义 HTTP 标头)
Could you technically call the string[] anything in the main method?(从技术上讲,您可以在 main 方法中调用 string[] 吗?)
What is the proper way of setting headers in a URLConnection?(在 URLConnection 中设置标头的正确方法是什么?)
How to overwrite http-header quot;Hostquot; in a HttpURLConnection?(如何覆盖 http-header “主机在 HttpURLConnection 中?)