开销与代码速度(java.io.File 数组与 java.lang.String 数组)

发布于 2024-11-06 09:18:12 字数 504 浏览 1 评论 0原文

只是想解决我在这里遇到的一个小困境。

目前,我正在开发一个应用程序,该应用程序涉及将文件列表收集到内存中以进行删除。现在,在这一点上,我认为 java.io.File 数组可能会占用太多内存,因为此上下文中的文件列表可能有数百个可能的条目。

我认为收集文件名列表并将它们存储为 java.lang.String 会更节省内存,而不是用 File 对象列表消耗过多的内存。现在,这是我的问题:考虑到要删除这些文件的目标,哪个会更便宜:

  1. 存储 File 对象数组而不是 String 对象,并调用 .delete();循环中的每一个(使用了太多内存)。
  2. 存储带有文件名的 String 对象数组,但对于循环的每次迭代,使用文件名列表创建一个新的 File 对象,并调用 .delete();在该文件上(这意味着每次循环迭代时,都会创建并销毁一个新的 File 对象 - 可能使用了太多的处理器能力)。

我想让程序尽可能快,所以这两种方法都有其优点,我只是想看看哪种方法的开销最小。提前致谢!

just trying to sort out a small delimma I'm having here.

Currently, I'm working on an application that involves gathering a list of files into memory, to be deleted. Now, at this point, I thought that a java.io.File array would perhaps take up too much memory, since the list of Files in this context could be in the hundreds of possible entries.

Rather than eat excessive amounts of memory up with a list of File objects, I figured that gathering a list of filenames and storing them as a java.lang.String would be cheaper to memory. Now, here's my problem: With the goal in mind that these files are to be deleted, which of these would be cheaper:

  1. Storing an array of File objects rather than String objects, and calling .delete(); on each one in a loop (too much memory used).
  2. Storing an array of String objects with the filenames, but for each iteration of the loop, create a new File object using the list of filenames, and call .delete(); on that file (which means each time the loop iterates, a new File object is created and destroyed--possibly too much processor power being used).

I want to make the program as fast as possible, so either approach has its merits, and I just want to see which of these has the least overhead. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

只是在用心讲痛 2024-11-13 09:18:12

java.io.File 表示有关文件系统中条目的文件名信息/元数据,它不包含文件的内容。

换句话说,像 new File("somelarge.txt") 这样的代码不会将 somelarge.txt 文件加载到内存中。

每个 File 对象包含的唯一真实数据是文件的 String 路径(以及 transient int prefixLength) - 考虑 File 类只是知道如何调用所有文件系统操作的字符串路径的包装器。

除非有其他一些要求,这里最好的选择是最容易阅读并最好地传达您的意图的代码。

The java.io.File represents the filename information/metadata about an entry in the filesystem, it does not contain the contents of the file.

In other words, code like new File("somelarge.txt") does not load the somelarge.txt file into memory.

The only real data that each File object contains is a String path to the File (along with a transient int prefixLength) - consider the File class merely a wrapper around the String path that knows how to invoke all of the filesystem operations.

The best choice here, barring some other requirements, is the code that is the easiest to read and conveys your intent the best.

葵雨 2024-11-13 09:18:12

我不想无礼,但让我首先引用“不惜一切代价避免过早优化”的口头禅。您的代码性能敏感吗?您有内存使用限制吗?循环中数百个 File 对象或数百个 File 对象创建听起来都不是那么糟糕。不过,如果您确实想优化,请使用分析器并使用这两种策略运行一些基准测试。我个人推荐 Netbeans Profiler

I don't want to be rude, but let me start by invoking the "Avoid Premature Optimizations at all costs" mantra. Is your code performance sensitive? Do you have memory usage constraints? Neither hundreds of File objects or hundreds of File object creations in a loop sounds that bad. Still, if you really feel like optimizing, go with a Profiler and run some benchmarks using both strategies. I would personally recommend Netbeans Profiler.

一向肩并 2024-11-13 09:18:12

文件主要是字符串的包装,并且比字符串本身多消耗 32 个字节。如果服务器中有 1000 个这样的内存,内存成本约为 70 美元/GB,那么它消耗的额外内存价值约为 0.22 美分。如果您领取最低工资,这大约相当于您 1 秒的时间。

除非您的设备内存有限,否则您可能无需担心任何消耗小于 1 MB 的内容。

A File is largely a wrapper for a String and consumes up to 32 bytes more than the String itself. If you have 1000 of these in a server where memory costs about $70/GB, the extra memory it consumes is worth about 0.22 cents. This is about the same as 1 second of your time if you are on minimum wage.

Unless you have a memory limited device, it is likely you don't need to worry about anything which consumes less than 1 MB.

清风无影 2024-11-13 09:18:12

现在,在这一点上,我认为
java.io.File 数组可能需要
占用了太多内存,因为列表
此上下文中的文件可能位于
数百个可能的条目。

除非您正在使用资源严重匮乏的系统,否则您不会遇到您认为存在的问题。

请记住,Java File 对象只是“文件和目录路径名的抽象表示。”因此,它代表任何文件的固定内存成本,无论有多大。如果您只处理数百个文件,则几乎肯定不会达到堆空间的任何限制。

如果您创建解决方案并发现使用分析和监视面临内存限制,那么此实现是您最后应该查看的地方之一。根本就没有那么多内存。

所以,简而言之,你应该编写你最理解并且将来能够维护的代码。简单的代码是你的朋友。

Now, at this point, I thought that a
java.io.File array would perhaps take
up too much memory, since the list of
Files in this context could be in the
hundreds of possible entries.

Unless you are working with a seriously resource-starved system, you do not have the problem that you think you have.

Remember that a Java File object is only "an abstract representation of file and directory pathnames." As such, it represents a fixed memory cost for any file, no matter how large. If you are dealing with only hundreds of Files, you are almost certainly not approaching any sort of limit on heap space.

If you create a solution and find that you are facing memory limitations using profiling and monitoring, this implementation is one of the last places that you should look. It's simply not that much memory.

So, in short, you should write the code that you understand the best and will be able to maintain in the future. Simple code is your friend.

月下凄凉 2024-11-13 09:18:12

对我来说,这听起来像是过早的优化,除非

  1. 您正在使用资源受限的移动设备,或者
  2. 数组中的元素(文件路径)数量可能非常大。

话虽如此,String 对象数组在内存和速度方面击败了 File 对象数组。造成这种情况的原因有几个:

  1. File 对象具有许多私有属性,包括但不限于

    • 私有字符串字段属性

    • 文件系统特定前缀的瞬态前缀长度字段

  2. File 对象实例化依赖于对 java.io.FileSystem 具体实现的静态引用, File 构造函数调用的它

    • 构造 File 对象至少需要调用 FileSystem.normalize() 和 FileSystem.prefixLength()(此外还要实例化其自己的对路径和前缀长度的私有引用。

的成本等于

n * (expense_of_constructor + avg_construction_of_individual_path_strings_off_filesystem)

expense_of_constructor = init_of_local_vars + expense_of_path_normalization + expense_of_prefix_length_computation

因此,创建 n 个 File 实例数组 n字符串路径名,成本仅为 就

 n * (avg_construction_of_individual_path_strings_off_filesystem)

空间而言,n File 对象数组的内存占用为:

 n * (avg_string_path_size + 32_bits_of_prefix_length + size_of_File_object_itself)

n String 对象数组

 n * avg_string_path_size

为简单 起见为了方便起见,我什至不会费心去进行任何估计,而且大多数情况下,这个细节只会在您使用非常有限的设备时才重要。 (以手机为例。)

Sounds like premature optimization to me unless

  1. You are working with a resource-constrain mobile device, or
  2. The number of elements (file paths) in the array might be very large.

Having said that, an array of String objects beat an array of File objects in terms of memory and speed. And there are several reasons for this:

  1. A File object has a number of private attributes, including but not limited to

    • a private String field attribute

    • a transient prefix length field for filesystem-specific prefixes

  2. A File object instantiation relies on a static reference to an concrete implementation of java.io.FileSystem, to which the File constructor(s) make calls to it

    • At a minimum, construction of a File object requires a call to FileSystem.normalize() and FileSystem.prefixLength() (in addition to instantiating its own private references to the path and prefix length.

So, the cost of creating an array of n File instances equals

n * (expense_of_constructor + avg_construction_of_individual_path_strings_off_filesystem)

expense_of_constructor = init_of_local_vars + expense_of_path_normalization + expense_of_prefix_length_computation

With an array of n String pathnames, the cost is just

 n * (avg_construction_of_individual_path_strings_off_filesystem)

In terms of space, the memory footprint of an array of n File objects will be:

 n * (avg_string_path_size + 32_bits_of_prefix_length + size_of_File_object_itself)

whereas an array of n String objects will be

 n * avg_string_path_size

For simplicity and convenience, I'd go with an array of Strings. I wouldn't even had bothered to do any estimation. Simpler tends to be better most of the time. And this minutia will only matter if you are working with a very constrained device (a mobile phone for instance.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文