-6

Code 1:

private static int myCompare(String a, String b) {

    /* my version of the compareTo method from the String Java class */

    int len1 = a.length();
    int len2 = b.length();

    if (len1 == len2) { 

        for (int i = 0; i < a.length(); i++) { 

            int intValueOfStringA = (int) a.charAt(i); 
            int intValueOfStringB = (int) b.charAt(i); 

            if (intValueOfStringA != intValueOfStringB) {

                return intValueOfStringA - intValueOfStringB;
            }
        }
    }
    return len1 - len2;
}

Code 2:

private static int stringCompare(String a, String b) {

    /* Direct implementation of the compareTo from the String Java class */


    int len1 = a.length(); 
    int len2 = b.length();
    int lim = Math.min(len1, len2); 

    char[] v1 = a.toCharArray(); 
    char[] v2 = b.toCharArray(); 

    int k = 0;
    while (k < lim) { 
        char c1 = v1[k];
        char c2 = v2[k];
        if (c1 != c2) { 
            return c1 - c2;
        }
        k++;
    }
    return len1 - len2;
}

Please ignore commenting on the type of access modifier used or the args made available to the method. I actually came up with Code 1 before being counseled by my friend to use the "compareTo" method from the String Class, which is reproduced here under Code 2. I wonder which piece of code is more effective though (w.r.t time and memory). Please advise.

I am trying to understand the Big O for both the code blocks and trying to elegantly improve efficiency. IMO Code 2 is relatively costly w.r.t memory usage. I want to confirm this. I apologize if my question is possibly alluding to the classic micro-optimization Vs. readable code debate.

2 Answers2

3

First: The two methods does two different things. stringCompare returns the alphabetic ordering of two strings which means it can be used to sort a set of strings alphabetically.

myCompare just returns -1 if the stings have different lengths, which means it can't be used for sorting. So comparing the performance of the methods does not make a lot of sense since they don't do the same thing.

For myCompare the behavior is so surprisingly different between strings of same length and strings of different length that I would consider it a bad design regardless of performance.

If you only want to compare strings for equality and don't care about sorting, then you should not compare the code to String.stringCompare() but rather String.equals() which have the same optimization for strings of different lengths.

Anyway, considering strings of the same length, both samples use the same basic algorithm for comparison, but the second sample copies the char arrays, which will require more memory. However both algorithms are O(n) operations, so the difference will be in constant factors like the possible overhead of calling length() multiple times and using charAt() rather than using array indexing. Such differences will depend on compiler and environment, so the only way you can find out is by benchmarking.

JacquesB
  • 59,334
1

Method 1 is quite efficient as far as execution time and memory usage are concerned. Unfortunately, it is completely useless, so efficiency doesn't help.

Method 2 is unnecessary inefficient for most pairs of strings, where probably the first or second character is already different, but you convert the complete strings to character arrays.

gnasher729
  • 44,814
  • 4
  • 64
  • 126