Alternative to File.lastModified ()?

My problem: I'm just trying to write a function that goes through a directory and gets the time when the last file was changed. I mean, there is a directory with files ~ 2500.5 megabytes. I don't care when each file has been modified, I just need the last modified timestamp. that is, File 1 - 1392567840 File 2 - 1392567841 File 2403 - 1392567849 File 7 - 1392567850

In this case, File 7 will be the last timestamp changed. However, I found that File.lastModified () is very slow, and it is also located in another stackoverflow entry, but for use with file copies.

So, for some preliminary research: File.lastModified () is painfully slow!

I looked through this forum and read suggestions on using multiple threads to execute all the latest modified queries, however I was wondering if there is an easier way to do this, since I just need the last modified value (so I think this is a much simpler problem, however I'm not sure if I can avoid individual searches :().

Some code for research:

public static void main(String[] args) {
    // TODO Auto-generated method stub

    long start;
    long end;
    long average = 0;
    int cnt = 10;
    for(int i = 0; i < cnt; i++) {
        start = System.nanoTime();
        System.out.println(GetLastModifiedTimeOfFiles("./data/"));
        end = System.nanoTime();
        average += (end - start);
    }
    System.out.println("The average time it took was: " + (average/(cnt * 1.0))/1000000 + " ms to complete!");

}

public static long GetLastModifiedTimeOfFiles(String path) {
      File folder = new File(path);
      File[] listOfFiles = folder.listFiles(); 
      StringBuilder sb = new StringBuilder();
      for(int i = 0; i < listOfFiles.length; i++) {
          sb.append(listOfFiles[i].getName() + " ");
      }
      System.out.println(sb.toString().length() + " chars");

      long time = 0;

      return time;
}

Results:

22ms average when using .getName ()

Average 631 ms when using .lastModified ()

So, 1) Why does the .getName () function execute ~ 30 times faster than .lastModified ()?

2) Is there any alternative approach since I just need the only recently changed file timestamp?

3) , , , ?

, , , , , , 2010 . , - .

!

+4

Source: https://habr.com/ru/post/1527206/


All Articles