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

查看:549
本文介绍了File.lastModified()痛苦地慢!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行文件的递归副本,例如 xcopy / D 我只想复制较新的文件目标文件(I 不能 xcopy 直接,因为我需要修改一些文件的复制过程)。

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

在java中,我使用 lastModified()

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

无论如何,复制文件不是一个选项,因为这比检查上次修改日期(通过网络复制)需要更多时间。

推荐答案

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

You need to determine why it is so slow.

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

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.

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

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.

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

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

这篇关于File.lastModified()痛苦地慢!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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