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