File.lastModified()痛苦地慢!(File.lastModified() painfully slow!)

系统教程 行业动态 更新时间:2024-06-14 16:57:39
File.lastModified()痛苦地慢!(File.lastModified() painfully slow!)

我正在做一个文件的递归副本,像xcopy /D我只想复制更新的文件目标文件(我不能直接使用xcopy ,因为我需要在复制过程中更改一些文件)。

在java中,我使用lastModified()来检查目标文件是否比源文件旧,而且速度非常慢。

我可以加快这个过程(可能使用JNI ??)? 是否有任何其他复制脚本可以更好地完成工作(复制新文件+正则表达式更改一些文本文件)?

无论如何都不能复制文件,因为这将比检查上次修改日期(通过网络复制)花费更多时间。

I'm doing a recursive copy of files and like xcopy /D I only want to copy newer files destination files (I cannot use xcopy directly since I need to alter some files in the copy process).

In java I use lastModified() to check if the destination file is older than the source file and it's very slow.

Can I speed up the process (maybe using JNI??)? Are there any other copy scripts that can do the job better (copy new files + regexp change some text files)?

Copying files anyways is not an option since that will take more time than checking last modified date (copying over the network).

最满意答案

你需要确定它为什么这么慢。

当您运行progrma时,您的进程的CPU利用率是多少。 如果它超过50%的用户,那么你应该能够选择你的程序,如果它低于20%你没有那么多你可以做。

通常这种方法很慢,因为您正在检查的文件是在磁盘上而不是在内存中。 如果是这种情况,您需要加快访问磁盘的速度,或者获得更快的驱动器。 例如,SSD可以快10到100倍。

批量查询可能会有所帮助。 您可以使用多个线程来检查lastModified日期。 例如,拥有固定大小的线程池并为每个文件添加任务。 线程池的大小决定了一次轮询的文件数。

这允许操作系统重新排序请求以适应磁盘上的布局。 注意:这在理论上很好,但你必须测试这是否会使你的操作系统/硬件上的速度更快,因为它可能会使速度变慢。 ;)

You need to determine why it is so slow.

When you are running the progrma what is the CPU utilisation of your process. If it more than 50% user, then you should be able to optmise your program, if its less than 20% there isn't so much you can do.

Usually this method is slow because the file you are examining is on disk rather than in memory. If this is the case you need to speed up how you access your disk, or get a faster drive. e.g. SSD can be 10-100x faster at doing this.

A bulk query might help. You can do this by using multiple threads to check the lastModified date. e.g. have a fixed size thread pool and add a task for each file. The size of the thread pool determines the number of files polled at once.

This allows the OS to re-order the requests to suit the layout on the disk. Note: This is fine in theory, but you have to test whether this makes things faster on your OS/hardware as its just as likely to make things slower. ;)

更多推荐

本文发布于:2023-04-13 11:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/3792a6f7a02c58a373d0268440e5cc74.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:痛苦   lastModified   File   painfully   slow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!