0

I read in another thread that StringBuilder is superior to String Concatenation, especially within loops. However, I solved the problem in two different ways, and the StringBuilder method took nearly double the runtime - The logic is otherwise the same, so why might this be?

class Solution {

public int reverse(int x) {
    
    boolean isNeg = false;
    
    if(x < 0){
        x *= -1;
        isNeg = true;
    } 
    
    ArrayList <Character> arrlist = new ArrayList<>();
    String reverse = "";
    StringBuilder sbdr = new StringBuilder();
    String s = Integer.toString(x);
    
    for(int i = 0; i < s.length(); i++) arrlist.add(s.charAt(i));
    
    while(arrlist.get(arrlist.size()-1) == '0' && arrlist.size() > 1) {
        arrlist.remove(arrlist.size()-1);
    }
    
    for(int i = arrlist.size()-1; i >= 0; i--) sbdr.append(arrlist.get(i));

    try {
        if(isNeg) return Integer.valueOf(sbdr.toString())*-1;
        return Integer.valueOf(sbdr.toString());
    } catch (Exception e){
        return 0;
    }

}

}

class Solution {

public int reverse(int x) {
    
    boolean isNeg = false;
    
    if(x < 0){
        x *= -1;
        isNeg = true;
    } 
    
    ArrayList <Character> arrlist = new ArrayList<>();
    String reverse = "";
    String s = Integer.toString(x);
    
    for(int i = 0; i < s.length(); i++) arrlist.add(s.charAt(i));
    
    while(arrlist.get(arrlist.size()-1) == '0' && arrlist.size() > 1) {
        arrlist.remove(arrlist.size()-1);
    }
    
    for(int i = arrlist.size()-1; i >= 0; i--) {
        reverse += Character.toString(arrlist.get(i));
    }
    
    try {
        
        if(isNeg) return Integer.valueOf(reverse)*-1;
        else return Integer.valueOf(reverse); 
        
    } catch (Exception e){
        return 0;
    }
    
}

}

  • 1
    You don't show us how you are measuring the time these are taking, but I suspect that what you are observing is an artifact of the way that you are benchmarking your code. Please read [How do I write a correct micro-benchmark in Java](https://stackoverflow.com/questions/504103), and make sure that your technique is correct. If you want us to look into this further, please update your Question to include the complete benchmark so that we can examine and run it ourselves. – Stephen C Dec 29 '21 at 07:44
  • @StephenC Thank you! The runtime was automatically output by LeetCode, I'm unsure how they benchmarked my code. Logically speaking, should my stringbuilder method have been faster than pure string concatenation? –  Dec 29 '21 at 08:04
  • I think it should. But there is some pretty inefficient code in there. For a start, you could use `String.toCharArray` to get an array of the characters, and then iterate the array from `length-1` to `0`. The `ArrayList` approach entails boxing every character in the string as a `Character` object ... – Stephen C Dec 29 '21 at 08:14
  • @StephenC thanks again, I'll try it again with just arrays. –  Dec 29 '21 at 08:19
  • See also `StringBuilder.reverse()` : https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#reverse() ... and why strings? see reverse digits : https://stackoverflow.com/a/4808804/2711811 –  Dec 29 '21 at 09:38

0 Answers0