1. import java.io.File;
  2. import java.util.Random;
  3. class FilePathPerformance {
  4. public static void main(String[] args) {
  5. String[] paths = {
  6. "/path/to/a/deeply/nested/file.txt", // Example 1: Deeply nested
  7. "C:\\path\\to\\a\\file.txt", // Example 2: Windows path
  8. "/home/user/documents/file.txt", // Example 3: Linux path
  9. "file.txt", // Example 4: Simple path
  10. "a/b/file.txt", // Example 5: Medium depth
  11. "../file.txt" // Example 6: Relative path
  12. };
  13. int iterations = 100000; // Number of iterations for performance measurement
  14. for (String path : paths) {
  15. long startTime = System.nanoTime(); // Start time
  16. File file = new File(path); // Create a File object
  17. for (int i = 0; i < iterations; i++) {
  18. file.exists(); // Measure the time to check if the file exists
  19. }
  20. long endTime = System.nanoTime(); // End time
  21. long duration = (endTime - startTime) / iterations; // Calculate average duration
  22. System.out.println("Path: " + path + ", Average duration: " + duration + " nanoseconds");
  23. }
  24. }
  25. }

Add your comment