How can I improve the run-time complexity of my method?

szk :

I wrote a function in Java that edit file name, and replace each space char into dash char. Currently I iterate all the files in a specific directory, iterate in each file name, creating a new file name, and replace the file in the directory. I guess that the current complexity is O(N*M) {N = number of files in directory, M = number of chars in each file}. Can anyone help me improve the run-time-complexity? Thanks

public static void editSpace(String source, String target) {

    // Source directory where all the files are there
    File dir = new File(source);
    File[] directoryListing = dir.listFiles();

    // Iterate in each file in the directory
    for (File file : directoryListing) {

        String childName = file.getName();
        String childNameNew = "";

        // Iterate in each file name and change every space char to dash char
        for (int i = 0; i < childName.length(); i++) {

            if (childName.charAt(i) == ' ') {
                childNameNew += "-";
            } else {
                childNameNew += childName.charAt(i);
            }
        }

        // Update the new directory of the child
        String childDir = target + "\\" + childNameNew;

        // Renaming the file and moving it to a new location
        if (!(childNameNew.equals(""))
                && (file.renameTo(new File(childDir)))) {

            // If file copied successfully then delete the original file .
            file.delete();

            // Print message
            System.out.println(childName + " File moved successfully to "
                    + childDir);
        }

        // Moving failed
        else {
            // Print message
            System.out.println(childName + " Failed to move the file to "
                    + childDir);
        }
    }

}
GhostCat salutes Monica C. :

I guess that the current complexity is O(N*M) {N = number of files in directory, M = number of chars in each file}. Can anyone help me improve the run-time-complexity?

Nobody can. You figured it yourself: when your task is to modify N file names that have like M chars to read(or modify), then you end up with NxM. There is no conceptual way to modify N file names based on their current names without each file and at each thing in there.

But what is possible: look carefully at your code, and see if you can improve the actual implementation.

You should start by relying much more on library methods. For example, you have String.replace() that allows you to turn all spaces into dashes with a single call. That shouldn't affect performance, but it allows your own code (having less code is mostly a good thing!). You could go one step further and look at streams to use even less code, see here.

But the real answer here: you are probably doing pre-mature optimisation. In the end, you are talking about something where the JVM needs to OS in order to make changes out there in the file system. There are zillions of aspects that influence overall, end to end performance for such a use case. It might be helpful to have more than one thread, so that can "process" file names from different directories in parallel for example.

On the other hand: creating a thread is a costly operation. And typically, it only helps you to speed up CPU intensive activities. Worse, multiple threads accessing the file system like that in parallel ... might actually slow down things, overall.

Meaning: depending on your overall setup, you might be able to speed up renaming files. Or not.

In the end, you are spending a lot of time and energy here. And the real question: is it really worth it?! Does it really matter to you whether your code will need 500 ms, or 1 sec, or 2 seconds? Depending on context it might, but maybe: it doesn't. That is the first thing to clarify. And when you figure that you really need the highest performance solution here, then you will have to invest real time into measuring what is going on, and doing experiments to find out which setting affects performance the most.

In other words: if you really care about performance here, you have a lot of low level details to look at. If you don't care about performance that much, I would throw away the java code and write 3 lines of python code, or Kotlin, or whatever you normally use for scripting, and go with that. Not because that code will be faster, but it will easier to read, write, and maintain. Because that is what matters when performance isn't your primary priority.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=164127&siteId=1